aspose file tools*
The moose likes Java in General and the fly likes weird syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "weird syntax" Watch "weird syntax" New topic
Author

weird syntax

Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
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 ]

SCJP 1.4
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
BTW, in some more object oriented languages than Java, Smalltalk for example, classes actually are objects, too.
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
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

Joined: Oct 12, 2000
Posts: 5093
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...


42
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Lukas Alamar
Ranch Hand

Joined: Mar 17, 2004
Posts: 68
thank you guys, now i have it figured out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: weird syntax
 
Similar Threads
creating a tiled watermark
What's wrong with the code???
Is transient allowed with static?
A small chicken and the egg situation!
overidden equals() error