aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner classes" Watch "Inner classes" New topic
Author

Inner classes

Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
JLS 8.1.2 : An inner class is a nested class that is not explicitly or implicitly declared static.
The following class NonInner is implicitly static. But fails to compile with JDK1.2.2
It does compile with jikes.
class Top {
static void method() {
class NotInner {
static int j;
}
}
}
Does it compile with JDK 1.3 ?

[This message has been edited by Deepak M (edited August 01, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
No, it will not!
Read further under the same section

Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language

That's why
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
What�s the difference between nested classes and inner classes if there�s any?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Marcela,
See my posting here which gives a summary of different types of classes. While you are there, check out the comment by Jim. That should clear things a little bit.
Ajith
( PS : The post I am talking about is towards the end of the thread ).
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
Originally posted by Ajith Kallambella:
No, it will not!
Read further under the same section
That's why
Ajith

Hi Ajith
Read further under the same section :
An inner class C is a direct inner class of a class O if O is the immediately lexically enclosing class of C and the declaration of C does not occur in a static context. A class C is an inner class of class O if it is either a direct inner class of O or an inner class of an inner class of O.
class Top {
static void method() {
class NotInner {
static int j;
}
}
}
In the above code, the declaration of NotInner occurs in a static context (in a static method in this case) !!! Therefore it is NOT an inner class.
Hence it should compile! and it does with jikes compiler ! :-)
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
Thanks Ajith
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Deepak,
Your code compiles fine if you comment out the static variable declaration. The error here has nothing to do with the class NotInner, however it is about the static variable.
I am using JDK 1.2.2 and I get this error - Only members of interfaces and top-level classes can be static, which clearly indicates the culprit here is j.
Ajith
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116

Well, the code above compiles and works with jikes. However,
it doesn't work with jdk 1.3.
Either jikes is more JLS 2 compliant or it has bugs!
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Apparently, unable to compile inner classes with static members is a bug. Here is the excerpt from Bug Parade.
Bug Id 4105615
Synopsis statics Should be Allowed in Inner Classes
State Closed, will not be fixed
Submit Date Jan 21, 1998

The prohibition on on static members in inner classes is motivated The real issue is what is the scope surrounding a static member. In an inner class, this scope is part of an enclosing instance. So a static variable's initialization
might depend on the particular instance of the enclosing class. And yet, the static variable must be initialized only once; there is not a distinct variable for each enclosing instance.
Handling this in its full generality requires adoption of different model of class nesting. In such a model, the inner classes of every instance would be distinct - they would have their own static state, their own distinct Class object and would be a distinct type for typechecking purposes.
Such a model was not deemed appropriate for the Java programming language.

Hope this clears the confusion,
Ajith
Disclaimer - Relax!. I am posting this only for the sake of clarification. This issue, howver, may not be of any significance to the SCJP exam!! I am almost certain they will not test you on nitty-gritty details like this!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm pretty sure that a class declared in a static context is not implicitly static. Those two concepts sure sound similar, and Sun probably could've save us all some confusion if they had defined the language that way, but they didn't. A class is only implicitly static if the JLS says it's implicitly static, as in the statements "Member interfaces are implicitly static" and "A member type declaration in an interface is implicitly static and public". The idea of a static context may sound similar, but nowhere in the JLS do they say it's the same as implicitly static. So, despite its name, the class NotInner is in fact an inner class, and therefore it can't declare any static members that are not compile-time constants.
As for Bug 4105615 - I don't think it's really a bug, technically. The guy was asking for a new feature that the language does not allow. They shot him down. It probably should've been marked RFE (request for enhancement) instead.


"I'm not back." - Bill Harding, Twister
Deepak M
Ranch Hand

Joined: Jul 10, 2000
Posts: 124
Originally posted by Jim Yingst:
I'm pretty sure that a class declared in a static context is not implicitly static.

I agree with Jim completely.
Heres my argument to support Jim
The JLS says :
An instance of an inner class I whose declaration occurs in a static context has no lexically enclosing instances. However, if I is immediately declared within a static method or static initializer then I does have an enclosing block, which is the innermost block statement lexically enclosing the declaration of I.
Which means the JLS DOES consides a class in a static context to be an INNER class.
[This message has been edited by Deepak M (edited August 01, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner classes
 
Similar Threads
Question about static class
Is there something called 'inner interface'?
static members in inner classes
Nested vs Inner
No inner class can have a static Member?