• Post Reply Bookmark Topic Watch Topic
  • New Topic

.forName and .class  RSS feed

 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
What is the difference between

Class.forName("appname"); and
HttpServletRequest.class;

Sujit
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two ways in Java to load a class into JVM.

1. By using the new Operation i.e. Test a = new Test();

2. By using Class.forName() i.e. Class.forname("Test"). This method call returns an Object of type Class.
From this Object, a new Instance can be created.
i.e. Class testClass = Class.forName("Test");
Test a = testClass.newInstance();

Every class that is loaded into the JVM has an Class object associated with it. i.e. HttpServletRequest.class retrieves the Class object of HttpServletRequest.
Read the api.

Hope this helps...


 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
(A)
>>2. By using Class.forName() i.e. Class.forname("Test"). This method call >>returns an Object of type Class.
>>i.e. Class testClass = Class.forName("Test");

(B)
>>HttpServletRequest.class retrieves the Class object of HttpServletRequest.

I can write from B
HttpServletRequest hSR = HttpServletRequest.class
and from A
hSR = Class.forName("HttpServletRequest")
I guess both A) and B) are same because both give a 'Class object'.
Sorry, still I don't understand how they are different.

sujit.
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K Sujit:
Thanks!
(A)
>>2. By using Class.forName() i.e. Class.forname("Test"). This method call >>returns an Object of type Class.
>>i.e. Class testClass = Class.forName("Test");

(B)
>>HttpServletRequest.class retrieves the Class object of HttpServletRequest.

I can write from B
HttpServletRequest hSR = HttpServletRequest.class
and from A
hSR = Class.forName("HttpServletRequest")
I guess both A) and B) are same because both give a 'Class object'.
Sorry, still I don't understand how they are different.

sujit.


I have corrected your code-

Class httpservlet = HttpServletRequest.class;
HttpServletRequest hSR = httpservlet.newInstance();


httpservlet = Class.forName("HttpServletRequest"));
hSr = httpservlet.newInstance();

Read the api.

By the way-HttpServletRequest is not the right example for Class.forName

The argument class to Class.forName() need to have a public,no argument constructor.
 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Your correction to my code is appropriate.

I read the api, but couldn't find anywhere .class constant/method, there is something like object.getClass() that can return the Class, but what is this .class.
In which class is this defined.

Sujit
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The argument class to Class.forName() need to have a public,no argument constructor.

I don't think that's correct. Class.forName returns a Class object, for which it is irrelevant what kind of fields and methods are contained in the class.

If you want to use the Class object to invoke newInstance() on it -in order to obtain an object of the class- then, yes, it needs to have a public, no-argument constructor.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a servlet question.
Moving to Java In General
 
Rajah Nagur
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the api, but couldn't find anywhere .class constant/method, there is something like object.getClass() that can return the Class, but what is this .class.
In which class is this defined.


It is Java's syntax of obtaining the Class Object for a class whose name is known at the compile time. This syntax will internally get converted to Class.forName() call in the bytecode. For more info check here

I don't think that's correct. Class.forName returns a Class object, for which it is irrelevant what kind of fields and methods are contained in the class.


Ulf Dittmer -
You are right. It is irrelevant for kind of constructors. My mistake
 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That clears the issue fully. Thnx.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless I missed it, we didn't answer why you would use Class.forName() instead of someObject.getClass() or SomeClass.class.

Class.forName() is dynamic. That is, we can have classnames at runtime that were never imagined at compile time. We can get classnames from configuration or even user prompts and instantiate objects:

This lets us build plugins and other flexible configurations. Pretty slick.

With SomeClass.class we have hard coded the class into our source, so we can't work with new ones at run time. Sometimes it's just fine, but sometimes we want that dynamic flexibility that comes only with Class.forName().
 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan James!

Let me ask you - Class.forName("DatabaseDriver"); is used with out assigning to any variable during DB access, I believe it is getting loaded to memory.

So if at compile time I know the driver name, would it be OK to use DatabaseDriver.class; instead of Class.forName

Sujit
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is another difference: When the class isn't in the classpath, Class.forName will throw a ClassNotFoundException, Foo.class a NoClassDefFoundError.

As it is more save to recover from Exception than from Errors, you should prefer the forName version if you want to gently handle the absence of a JDBC driver, for example.

The .class version, on the other hand, has the advantage that the class name gets checked at compile time, and that it is more refactoring friendly.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ilja]: As it is more save to recover from Exception than from Errors, you should prefer the forName version if you want to gently handle the absence of a JDBC driver, for example.

I don't really buy that here. If the required class file cannot be located at run time, I can't see any reason why catching NoClassDefFoundError from the class literal would be any more (or less) safe than catching ClassNotFoundException from Class,forName(). Except that the latter may require less time arguing with fellow developers who think that catching Errors is unthinkable. The basic problem is the same in both cases. If you have a viable backup plan for what to do if the driver is unavailable, you can just catch the exception and proceed with that plan. If you don't have a backup plan, then either error/exception is equally fatal.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!