• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll find some explanations in the Java Reflection Tutorial.
 
Muthukrishnan Manoharan
Ranch Hand
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Muthukrishnan : what is your jdk version?
@Sunny :what is your jdk version?
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Version is not making any change for the output.

I have checked with 1.4, 1.5 and 1.6
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your kind reply Rob
 
Sunny Bhandari
Ranch Hand
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But how come I get the output as;



for the code:

 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 448
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried with SUN/Oracle 1.4,1.5 and 1.6 but same output

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic