GeeCON Prague 2014*
The moose likes Servlets and the fly likes .forName and .class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark ".forName and .class" Watch ".forName and .class" New topic
Author

.forName and .class

K Sujit
Ranch Hand

Joined: Mar 23, 2005
Posts: 33
Hi all,
What is the difference between

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

Sujit


I wish java run in my veins.
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
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...




You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
K Sujit
Ranch Hand

Joined: Mar 23, 2005
Posts: 33
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

Joined: Nov 06, 2002
Posts: 239
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

Joined: Mar 23, 2005
Posts: 33
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
Marshal

Joined: Mar 22, 2005
Posts: 42056
    
  64
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.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Not a servlet question.
Moving to Java In General


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: .forName and .class