File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference static var of static nested from Outer 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 "Reference static var of static nested from Outer" Watch "Reference static var of static nested from Outer" New topic
Author

Reference static var of static nested from Outer

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

Why does line 1 not compile?
Thanks.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

What error are you getting? This compiled fine for me...


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
The line causing an error is commented out.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Cathy
No error for me either.
Ofcourse with line 1 uncommented and removing line 2
Cheers
Harwinder
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
It doesn't compile for me with the following listing:

I'm using JDK 1.4.2. And you guys?
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
Ok, I admit, I'm using an older version 1.2.2. That's all I got at work. But I've got 1.4.2 installed on my PC at home and will go back and verify the results in a few hrs.
But I trust Cathy & Vad on that.
Cheers
Harwinder
Dan Gun
Greenhorn

Joined: Aug 06, 2003
Posts: 26
JDK 1.4.1 came up with this compiling error:
"Illegal enclosing instance specification for type OuterReferVarOfStaticInner.StaticInner"


SCJP 1.4
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Originally posted by Cathy Song:

Why does line 1 not compile?
Thanks.

When referring to a static class whether it be top level or nested, you cannot use an instance variable to get to it. For example, if you want to use a method from the Math class, you simply use Math.<method name>. You do not instantiate a static class. Same rule applies here even though it is nested. Use the fully-qualified class name to refer to the variable in the static class: OuterReferVarOfStaticInner.StaticInner.i
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi Chris,
Math class is NOT static. And top-level classes MAY NOT be static. The reason you cant create an instance of Math is because its constructor is private.
You'll get the following error:
"Math() has private access in java.lang.Math"
The reason you can access the methods of Math class without creating an instance of Math is because the *methods* are static, that means thay are NOT associated with an instance of the class, rather the class itself. That is why static methods are also called class methods.

Thanks.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
With Java 1.4.2_02, I get the same error message as Vad mentioned above.
Btw, you could use:
int j = new OuterReferVarOfStaticInner.StaticInner().i; //line 3 ok
OR
int j = new StaticInner().i; //line 4 ok
Cheers
Harwinder
[ November 12, 2003: Message edited by: Harwinder Bhatia ]
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
For a static member class, you don not use the instance of the enclosing class to access it.
Harwinder Bhatia
Ranch Hand

Joined: Oct 17, 2003
Posts: 150
All I have read is that for a static nested class, you don't need an instance of the enclosing class. But, I've "not" read anywhere that you absolutely 'cannot' (compile-time error) specify an instance of the outer class.
Could somebody please clarify?
Thanks
Harwinder
pallavi pandey
Greenhorn

Joined: Nov 06, 2003
Posts: 12
The static nested class is tied only to the outer class, not an instance of the outer class.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Pallavi, Chris and Dan.
__________________________________________________________________
This is from JLS 15.9

ClassInstanceCreationExpression:
new ClassOrInterfaceType ( ArgumentListopt ) ClassBodyopt
Primary.new Identifier ( ArgumentListopt ) ClassBodyopt
Class instance creation expressions have two forms:
* Unqualified class instance creation expressions begin with the keyword new. An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top-level (�7.6), member (�8.5, �9.5), local (�14.3) or anonymous class (�15.9.5).
* Qualified class instance creation expressions begin with a Primary. A qualified class instance creation expression enables the creation of instances of inner member classes and their anonymous subclasses.

And from JLS 15.9.1

Otherwise, the class instance creation expression is a qualified class instance creation expression. It is a compile-time error if Identifier is not the simple name (�6.2) of an accessible (�6.6) non-abstract inner class (�8.1.2) T that is a member of the compile-time type of the Primary. It is also a compile-time error if Identifier is ambiguous (�8.5). The class being instantiated is the class denoted by Identifier.

Thus it seems that the compiler is getting more and more JLS compliant.
[ November 12, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference static var of static nested from Outer