• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

passed SCJp1.5 with 95%. | earlier topic static class - nested class : are they same?

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS-8.1.2 mentions that.

It is a compile-time error to refer to a type parameter of a class C within a static initializer of C or any class nested within C.

Is the "Any class nested within C", meant to be a static inner class? I am able to refer the type parameter of a class in a regular inner class, method local inner class, anonymous inner class. But not in static inner class. So does "Nested class" mean "Static Class"??



I get compilation error in static class
regards
Binu K Idicula
[ July 21, 2006: Message edited by: Binu K Idicula ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"...within a static initializer of C or any class nested within C."

This is a bit ambiguous. It may seem like it could mean

"...within a static initializer of C, or within any class nested within C."

but what they meant here was

"...within a static initializer of C, or within a static initializer of any class nested within C."

You are not within any static initializer, and so this quote does not apply.

So why do you get a compile error? That comes from the sentence just before the one you quoted:

"It is a compile-time error to refer to a type parameter of a class C anywhere in the declaration of a static member of C or the declaration of a static member of any type declaration nested within C."

InnerClassStatic is a static member of class InnerClassCannotAccessTypeParameter, therefore it is a compile-time error to refer to type parameter T within InnerClassStatic.

By the way, since you're reading the JLS you should not use the term "inner" to describe a static nested class. The two terms are completely incompatible. An static class is never inner - not using the JLS definition of "inner". Various other books incorrectly use "inner" as a synonym of "nested", but that's not how the JLS defined the term, and if you don't follow their definition while reading the JLS you will encounter other logical inconsistencies.
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim, Thank you very much for the explanation.

But inner class variants(Regular Inner class, Method Local, Anonymous)cannot have a static initializer. So why JLS put this quote then?

Agreed that a static nested class is a static member and hence the compilation error, when referring to type T.
[ April 21, 2006: Message edited by: Binu K Idicula ]
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Binu]: But inner class variants(Regular Inner class, Method Local, Anonymous)cannot have a static initializer. So why JLS put this quote then?

Well, the quote is still accurrate. I guess it would have been more specific to say

"...within a static initializer of C or any static class nested within C."

but the statment is true even without the extra "static". The stattement does not attempt to be a complete list of all rules regarding nested classes, and yes the fact that nonstatic nested classes cannot have static initializers still is true - whether they restate it here or not.
 
Binu K Idicula
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I cleared SCJP 1.5, with 95% yesterday. 21-July-2006 at Banglore. Thread questions took more time ,and I missed 3 questions and the score sheet says 1 from fundamentals, 1 from [decalaration,initialization and scoping], and 1 from API knowledge.
Thanks to everyone for support.
regards
Binu K Idicula
SCJP 1.2, SCJP 1.5, OOAD-UML
[ July 21, 2006: Message edited by: Binu K Idicula ]
reply
    Bookmark Topic Watch Topic
  • New Topic