Forums Register Login

.forName and .class

+Pie Number of slices to send: Send
Hi all,
What is the difference between

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

Sujit
+Pie Number of slices to send: Send
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...


+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Not a servlet question.
Moving to Java In General
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
That clears the issue fully. Thnx.
+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
[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.
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 686 times.
Similar Threads
.forName and .class
Class.forName vs Class literal
reg class.forName()
calling java inside a servlet
Class.forName
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:13:37.