Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.forName and .class

 
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
Pie
Posts: 42967
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic