Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:32:38+00:00 2026-05-27T16:32:38+00:00

I am passing an interface as anonymous implementation to a different object like this:

  • 0

I am passing an interface as anonymous implementation to a different object like this:

public interface Interface {
    public int convert (int a);
}



public static void main(String[] args) throws IOException, InterruptedException {

    final int[] array = {1,6,3,5,7,8,4,0,3};
    Interface inter = new Interface() {         
        public int convert(int a) {
            int result = a;             
            for (int i = 0; i < array.length; i++) {
                a=a+array[i];
            }               
            return a;
        }
    };

    SomeObject ty = new SomeObject ();
    ty.Test(7, inter);
}

public class SomeObject {   

    public void Test(int number, Interface inter) {             
        System.out.println(inter.convert(number));
    }
}

My question is: how does it work? How does SomeObject know about the array which is not passed directly to the object (array is not a member of the anonymous class).

Update
(sorry for late update)

what about member vars or methods methods that are used in the anonymous class? they are not final

Interface inter = new Interface() {         
    public int convert(int a) {
        int result = a + someMemberVar;             
        for (int i = 0; i < array.length; i++) {
            a=a+array[i];
        }               
        return a;
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:32:39+00:00Added an answer on May 27, 2026 at 4:32 pm

    My question how does it work? How SomeObject knows about the array
    which is not passed directly to the object (array is not a member of
    the anonymous class).

    • Answer: It doesn’t; unless you declare array as final.
    • Reasoning: It works (if array is final) because since it’s final, it can be treated like a constant, since the anonymous function knows
      that the array cannot be changed.

    what about member vars or methods methods that are used in the
    anonymous class? they are not final

    • Answer: Instance variables and methods (as well as static variables and methods) are in scope anyway, when the anonymous class is created.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface class public interface AsyncTaskExecuteCommand { public Object executeCommand(String jsonLocation) throws
i've a stupid questiona about passing pointer. I've this: @interface MyClass : NSObject myobj*
I have following code public interface IEntity { // Properties int Id { get;
When calling JsonConvert.SerializeObject I am passing in an object that implements an interface. It
Interface IView { List<string> Names {get; set;} } public class Presenter { public List<string>
public interface IProcessor<T> { void Process(T instance); } foreach(AbstractType instance in myClass.SomeCollection) OnProcess(instance); public
Service Interface: public List<UserAccount> getUserAccounts(); public List<UserAccount> getUserAccounts(ResultsetOptions resultsetOptions, List<SortOption> sortOptions); Service Implementation: public
Assume my WCF interface declared like that: [ServiceContract] public interface IManagementConsole { [OperationContract] ConsoleData
The case is: [ServiceContract] public interface IInfo { [DataMember] int Id{get;set;} } [DataContract] [KnownType(typeof(Legal))]
If I am passing an object to a method, why should I use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.