Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

weird syntax

 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyClass.class
MyClass.class.getName()

can anyone explain this .class?
how can you use the dot operator on a class name instead of an object instance!

(if you have some useful links, please don't hesitate to post them)

thanx,
any help would be much appreciated

[ July 23, 2004: Message edited by: Lukas Alamar ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the syntax for using the class literal and evaluates to the Class object for the class. See http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, in some more object oriented languages than Java, Smalltalk for example, classes actually are objects, too.
 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, suppose i have not instatiated any object of type MyClass yet, does the Class instance for MyClass already exist? no, i think...so is it instantiaded the first time you create a MyClass obj?
Is there just ONE instance of Class relating to MyClass even if there are 200 MyClass objects?

thanx
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
BTW, in some more object oriented languages than Java, Smalltalk for example, classes actually are objects, too.


there's a class Class in Java
Java class Objects ARE objects... If they weren't you couldn't call the getName() method on them...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lukas Alamar:
ok, suppose i have not instatiated any object of type MyClass yet, does the Class instance for MyClass already exist? no, i think...so is it instantiaded the first time you create a MyClass obj?


The Class object for MyClass is created when MyClass is loaded. When this happens, exactly, depends on how MyClass is used. Basically, MyClass is loaded as soon as any code refers to MyClass. If class X has member variables of type MyClass, then MyClass is loaded as soon as X is, even if those variables aren't initialized with new objects. On the other hand, if MyClass is only mentioned inside of a method y() of class X, then MyClass won't be loaded until y() is called.

But as far as MyClass.class goes: you will never find that ".class" is null. Java always loads classes just before they're used.


Is there just ONE instance of Class relating to MyClass even if there are 200 MyClass objects?


Yes, just one, whether there are zero or 200 MyClass objects.

Unless, of course, MyClass is loaded by more than one ClassLoader. But that's an advanced topic, and you won't run into it accidentally.
 
Lukas Alamar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys, now i have it figured out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic