• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Reference static var of static nested from Outer

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why does line 1 not compile?
Thanks.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error are you getting? This compiled fine for me...
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line causing an error is commented out.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cathy
No error for me either.
Ofcourse with line 1 uncommented and removing line 2
Cheers
Harwinder
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't compile for me with the following listing:

I'm using JDK 1.4.2. And you guys?
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JDK 1.4.1 came up with this compiling error:
"Illegal enclosing instance specification for type OuterReferVarOfStaticInner.StaticInner"
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a static member class, you don not use the instance of the enclosing class to access it.
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static nested class is tied only to the outer class, not an instance of the outer class.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic