Page 1 of 1

About Question enthuware.ocpjp.v7.2.1369 :

Posted: Fri Sep 27, 2013 7:21 am
by Wisevolk
Hi, So the only way to use doA(0 declare in the anonymous class is to use it whitin that declaration ?

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Fri Sep 27, 2013 7:59 am
by admin
Not sure what you mean. Can you please put what you mean in code?
-Paul.

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Fri Sep 27, 2013 8:14 am
by Wisevolk
in your code the method declare at //2 can only be use in the scope of the new inner() declaration at //1 because it's an anonymous class ?
package test;
public class TopClass {
public Inner inner1 = new Inner() //1
{
        public void doA(){  // 2
System.out.println("doing A");
}     
};
public void doA() { inner1.doA(); }
}
class Inner {    int value; }

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Fri Sep 27, 2013 8:44 am
by admin
Yes, but not just because it is an anonymous inner class. It is also because the declaration of doA() is not present in class Inner. So you cannot call doA() on a variable of type Inner.

It is really same as introducing a new method in a sub class. For example, if you have two classes InputDevice and KeyBoard, where KeyBoard extends InputDevice and KeyBoard has an extra method getNumberOfKeys(), you can't call this method on a variable of class InputDevice. i.e.

InputDevice id = new KeyBoard() ; //This is fine
id.getNumberOfKeys(); //This will not compile.

Of course, within the KeyBoard class, you can call getNumberOfKeys() on 'this'.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Sun Sep 29, 2013 7:33 am
by Wisevolk
Thnks

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Wed Nov 05, 2014 12:36 am
by vijayanand
Does this mean that the anonymous inner class inner1 is like extending class Inner with its doA() method ?

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Wed Nov 05, 2014 1:13 am
by admin
Yes, that is correct.

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Wed Nov 05, 2014 7:19 am
by vijayanand
What i understand is anonymous inner class can be created only in a code block (inside method, constructor, or intialization block).

How does compiler allows creating anonymous inner class inside top-level class?

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Thu Nov 06, 2014 12:32 am
by admin
Sorry, I don't understand your question.

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Tue Jan 03, 2017 2:28 pm
by jagoneye
Also note guys non-overrided methods inside anonymous class can be called!
It can be done by placing the call inside the overrided method.

Code: Select all

class InnerTest1
{
 public static void main(String args[])
   {
InnerTest1 i1 = new InnerTest1(){
          public void callme()
          {
              m1();
          }
          void m1(){
              System.out.println("m1");
          }
      };
      i1.callme();
   }
public void callme()
   {
   }
}
I think this should also be added to the explaination if it is worth it! :)

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Sun Jun 10, 2018 3:56 am
by Harvey Manfrenjensen
>>>Observe that there is no way doA() can be accessed.
fyi...

Code: Select all

class TopClass
{
    public Inner inner1 = new Inner()
    {
        public Inner doA(){  
            System.out.println("doing A"); 
            return this;
        }
    }.doA();  // call doA() here

    public void doA() { inner1.doA(); }
}

class Inner
{
   int value;
}

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Sun Jun 10, 2018 4:18 am
by admin
Assuming that you are trying to show a counter example, have you even tried to compile your code?

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Sun Jun 10, 2018 11:57 am
by Harvey Manfrenjensen
you are right, it does not compile, i took it from the explanation of question,
which show inappropriate call of a new method of anonymous class.
Here is example, that compiles.

Regards, Paul

Code: Select all

class TopClass
{
    public Inner inner1 = new Inner()
    {
        public Inner doA(){  
            System.out.println("doing A"); 
            return this;
        }
    }.doA();  // call doA() here
}

class Inner
{
   int value;
}

Re: About Question enthuware.ocpjp.v7.2.1369 :

Posted: Sun Jun 10, 2018 12:17 pm
by admin
Yes, this compiles but this is not the doA() that is given in the problem statement.

Your code does show an interesting way to invoke a method of an inner class though.

regards,
Paul.