wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes in static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes in static context" Watch "Inner classes in static context" New topic
Author

Inner classes in static context

Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
I have read in many books that all local classes are also inner classes.
A local class declared in static context cannot refer to its enclosing class instance variables or methods.
For example:
class A
{
int a = 5;
static void method()
{
class B
{
void foobar() {System.out.println(A.this.a);} // Is Illegal
}
}
}
As such class B's instance is not associated with A's instance.
Then is the statement true "All inner class objects r ALWAYS associated with its enclosing class instance" ?
This question has been asked in many of the mock tests. Probably something wrong in my understanding of the exact terminology.
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Then is the statement true "All inner class objects r ALWAYS associated with its enclosing class instance" ?
This question has been asked in many of the mock tests. Probably something wrong in my understanding of the exact terminology.[/QB]
---------------------------------------------------------------------------
This statement is write.
cz if it is non-static inner class then we r instantiating it as:
<enclosing object reference>.new
if local class and anonymous class then we r instantiating them by calling a method in which they r declared.
<enclosing object reference>.methodname()
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Then is the statement true "All inner class objects are ALWAYS associated with its enclosing class instance" ?

No, the statement is false. According to JLS 8.1.2 Inner Classes and Enclosing Instances,
An instance of an inner class I whose declaration occurs in a static context has no lexically enclosing instances.

Many books and mock exams do not take into consideration the case of a local class or anonymous class declared in a static context.
By the way, here are all possible static contexts (from JLS 8.1.2),
a static method, a static initializer, the variable initializer of a static variable, or an explicit constructor invocation statement

Probably something wrong in my understanding of the exact terminology.

No. Your brain is working fine. You understand it.
[ September 03, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

Here are the two inner classes, resulting from the compilation.
09/03/2003 09:54a 294 Test$1$A.class
09/03/2003 09:54a 223 Test$1$B.class
09/03/2003 09:54a 272 Test.class
Let�s disassemble the inner class files and look inside.

The inner class A has a field of type Test named this$0 which is a reference to the enclosing instance. This field was added by the compiler.
The inner class B, declared in a static context, does not have such a synthetic field. Hence, no enclosing instance.
[ September 03, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Also in JLS 15.9.2 Class Instance Creation Expressions: Determining Enclosing Instances

Let C be the class being instantiated, and let i the instance being created. If C is an inner class then i may have an immediately enclosing instance.
If C is a local class (�14.3), C must be declared in a method declared in a lexically enclosing class O.
If C occurs in a static context, then i has no immediately enclosing instance.

There are analogous statements for anonymous classes in the same section.
[ September 03, 2003: Message edited by: Marlene Miller ]
Gopal Shah
Ranch Hand

Joined: May 17, 2003
Posts: 65
Thanks a lot Marlene for the prompt reply.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I just thought of a nice way to show that a local or anonymous class declared in a static context does not have an enclosing instance.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Here is a related idea. This example compares the enclosing instance of the inner class to the current instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes in static context