All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: About Question com.enthuware.ets.scjp.v6.2.359 :
PostPosted: Wed Feb 22, 2012 1:04 pm 
Hello,
the Explanation box for this question says : "Unlike popular belief and unlike mentioned in various books, anonymous class can never be static. Even if created in a static method."

If that were true, then it would be possible for the anonymous class to access a non-static member (such as an instance variable) declared inside the outer class, as in this code:

class A
{
}
public class TestClass
{
int outerInt = 7;
public class A
{
public void m() { }
}
class B extends A { }
public static void main(String args[]) {
new TestClass().new A() {
public void m() {
System.out.println("OuterInt is "+outerInt);
}
};
}
}

But this code won't compile because the compiler will detect an access of a non-static resource from a static context.
Perhaps didn't get the meaning of that explanation correctly; please shed some light on this. :cry:


Top
  
 
 Post subject: Re: About Question com.enthuware.ets.scjp.v6.2.359 :
PostPosted: Wed Feb 22, 2012 1:56 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
In this case, the inner class is unable to access outerInt, not because the inner class is static but because the inner class created in a static context (in this case a static method.)

There is not just one rule that applies while determining whether you can access a field or not. There are multiple rules that determine valid access.

An inner class can only access those fields that are available in its context. In this case, the main method is static and it doesn't have access to outerInt, so the inner class created in the main method also doesn't have access to outerInt.

This doesn't mean that the inner class itself is static.

HTH,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question com.enthuware.ets.scjp.v6.2.359 :
PostPosted: Fri Apr 21, 2017 5:07 am 
Offline

Joined: Sat Mar 01, 2014 1:48 pm
Posts: 29
According to the commentary for option 3:
Quote:
Anonymous classes are implicitly final.

Using reflection to test that, it doesn't seem to be the case, (assuming I'm testing what I think I am!)

Code:
import java.lang.reflect.*;

class AnonymousTest{
  class A{}
  static void report(Object o){
    Class myClass = o.getClass();
    System.out.printf("%s\n", Modifier.toString(myClass.getModifiers()));
  }
  public static void main(String[] args){
    report((new AnonymousTest()).new A(){});   //static but not final
  }
}


although I don't think where would in practice be any way to extend the anonymous class.


Top
 Profile Send private message  
 
 Post subject: Re: About Question com.enthuware.ets.scjp.v6.2.359 :
PostPosted: Fri Apr 21, 2017 5:48 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6757
But it is final as per the specification: https://docs.oracle.com/javase/specs/jl ... tml#15.9.5


Top
 Profile Send private message  
 
 Post subject: Re: About Question com.enthuware.ets.scjp.v6.2.359 :
PostPosted: Fri Apr 21, 2017 5:53 am 
Offline

Joined: Sat Mar 01, 2014 1:48 pm
Posts: 29
admin wrote:
But it is final as per the specification: https://docs.oracle.com/javase/specs/jl ... tml#15.9.5


ok. Thank you.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum