aspose file tools*
The moose likes Java in General and the fly likes Difference between Class.forName( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Difference between Class.forName("ClassName") and ClassName.class" Watch "Difference between Class.forName("ClassName") and ClassName.class" New topic
Author

Difference between Class.forName("ClassName") and ClassName.class

Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

Hi all,

May I know how does Class.forName("ClassName") different from ClassName.class. For example, I tried to execute the following




My output :



Also, I tried the following,


My output:


So doesn't it load the class when I say ClassName.class, but loads only when I use the Class Object to get constructors(cls.getConstructors()) or methods?

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You'll find some explanations in the Java Reflection Tutorial.


[My Blog]
All roads lead to JavaRanch
Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

Thanks Chris,

I actually followed the tutorial first and tried to execute an example from my understanding.


But the output I get from using Class.forName("ClassName") and ClassName.class are different as I said above. Is it normal or am I missing something.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Muthukrishnan Manoharan wrote:Thanks Chris,

I actually followed the tutorial first and tried to execute an example from my understanding.


But the output I get from using Class.forName("ClassName") and ClassName.class are different as I said above. Is it normal or am I missing something.


Please recheck the output again carefully.

I am getting the same out put for both.



Java Experience
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

@Muthukrishnan : what is your jdk version?
@Sunny :what is your jdk version?
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

Version is not making any change for the output.

I have checked with 1.4, 1.5 and 1.6
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

The main difference is in the required availability of the class. With ClassName.class the class needs to be available at compile time. If the class is missing while you are compiling you will get a compiler error. With Class.forName("ClassName") the class doesn't need to be there at compile time, only at runtime.

Another important difference is the generic type. ClassName.class returns a Class<ClassName>. Class.forName("ClassName") returns a Class<?> - the actual type is not known.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Sunny Bhandari wrote:
I am getting the same out put for both.


As muthu mentioned the output is different for me.
Muthukrishnan Manoharan
Ranch Hand

Joined: Aug 27, 2008
Posts: 91

Hi all,

Please ignore the previous code. Since I am creating newInstance from the class obtained, it is obvious that it executes the static content.

But this is my actual code and corresponding output. My version of JDK is 1.5.

Example1:


output:


Example2:


output:


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Muthukrishnan Manoharan wrote:Since I am creating newInstance from the class obtained, it is obvious that it executes the static content.

dont worry, I already ignored that
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Class.forName actually loads the class. ClassName.class doesn't load the classbecause it's not necessary to do so. If you instantiate the class or access a static member (method, field) then the class is loaded, but not just by calling ClassName.class.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well rob, one query : if ClassName.class doest not load a class then how come below code is working. please can you explain?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Guess I was wrong. It's loaded all right (as can be seen when running the JVM with -verbose), just not initialized. That only happens when instantiating or accessing a static member.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thank you for your kind reply Rob
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

But how come I get the output as;



for the code:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Which JVM are you using? Not just the version but also the vendor. Because my Sun / Oracle 1.6.0_23 JVM doesn't initialize the class with that code.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

I have tried with SUN/Oracle 1.4,1.5 and 1.6 but same output

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Class.forName("ClassName") and ClassName.class