• 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

What does "ClassName.class" mean? What are static classes?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the cryptic title.

Here's a line of code:



Question 1) DataLine.Info is a static class, what does that mean exactly?

Question 2) What exactly is ".class"? it's not an instance variable...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question 1) DataLine.Info is a static class, what does that mean exactly?



It's an inner class that in *not* bound to an instance of the outer class -- meaning that it can't access non-static members of the outer class, as there is no instance. This is used in cases where there is no need for an instance of the outer class, or if it isn't available.

Question 2) What exactly is ".class"? it's not an instance variable...



It's a special variable, that looks like an instance variable -- but obviously can't be one because the class name is not a reference variable. Just think of it as something the compiler takes care of. It is a reference to the Class object that can be used to introspect the class.

Henry
[ September 22, 2007: Message edited by: Henry Wong ]
 
Tarek Khojah
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! That was pretty quick

So can outer classes be static? And why isn't the special "class" variable mentioned in the API docs?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
It's a special variable



In fact, it's not a variable at all. It's a "class literal" - just as

"foo"

is a String literal, or

42.0

is a double literal.
 
Wanderer
Posts: 18671
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Henry]: It's an inner class

Oh, so you're one of the (former) Sun employees responsible for muddying the waters about the definition of terms like "inner" and "nested". It would have made a lot of sense if Sun had officially adopted definitions that made inner classes and nested classes the same thing - but they didn't. By definition, a static nested class is not an inner class. Because inner classes are, by definition, not static. All classes are either top-level classes or nested classes. All nested classes are either static classes or inner classes. Any time someone refers to a static inner class, assume they mean a static nested class - but treat everything else that person says about "inner" classes with suspicion, because apparently they've rejected the official definition, and who can tell what they mean in that case.

I should note that Henry is among my favorite JavaRanchers, and I have great respect for him. But terminology is important. It's unfortunate that Sun didn't choose more logically intuitive terminology, but it's been, what, eight years or so since Sun published the Inner Classes Specification? OK, bad example, since it was so logically inconsistent and poorly-named. Seven years since they published the JLS second edition, which incorporated most of the inner classes spec while removing the self-contradictory crap. Yes, the compiler still sometimes issues error messages which were evidently written by engineers unfamiliar with the specs, but oh well.

[Tarek]: So can outer classes be static?

Outer classes can never be static. Or rather, the term "static" is irrelevant to outer classes - though I would say that outer classes are more like static nested classes than they are like inner (non-static) classes. When applied to classes, "static" is only meaningful in defining how a nested class relates to its containing class.

And why isn't the special "class" variable mentioned in the API docs?

Where should it be mentioned? It's not an attribute of any one class - it's an attribute of all classes. They could define it in every single class, but that seems wasteful. And they can't define it in Object, because it's different for every class. It's like a static field, although technically it's not a field or variable at all. Anyway, it's defined in the Java Language Specification, section 15.8.2.
[ September 22, 2007: Message edited by: Jim Yingst ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oh, so you're one of the (former) Sun employees responsible for muddying the waters about the definition of terms like "inner" and "nested".



Hey, it took me years before I stopped using "instance" and "reference" interchangeably... Give me another few years for "inner" and "nested" please...

Henry
 
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
Fair enough. Sun in general has a long history of internal inconsistency on this matter, and I can well imagine that internally there were enclaves that developed a more logically self-consistent terminology than the one Sun officially chose.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[JY:]although technically it's not a field or variable at all.

Then is it something like a literal ? In other words if one writes:

int importantInt = 0x00ff;//

then the beginner coder may try to figure out how to change that literal even though it is just something that is brought in a load and doesn't have an existence in the program.

I was reading class.java a few days ago. I can see where it would take some compiler science to understand the goals, objectives and methods of that file and the ops in it.
 
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
[NJ]: Then is it something like a literal ?

Very much like a literal. So much, that it's called a "class literal", as indicated by the title and text of JLS 15.8.2 which I previously linked to.

I can't tell what any of your subsequent comments mean, or how they relate to anything said previously in this thread, so I will stop here.
 
reply
    Bookmark Topic Watch Topic
  • New Topic