*
The moose likes Java in General and the fly likes How Class.ForName() actually creates new object internally passed in argument ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How Class.ForName() actually creates new object internally passed in argument ?" Watch "How Class.ForName() actually creates new object internally passed in argument ?" New topic
Author

How Class.ForName() actually creates new object internally passed in argument ?

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Hi,

how Class.forName("java.lang.String"); method creates new object.

it might be somehow java reflection.

i looked into sun src in class Class but not very much clear.

could anyone help me out how internally by step by step it works .. I am very curious about it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, in fact it doesn't create a new object. All it does is to load the class whose name was its parameter. (In your example it loads the java.lang.String class.) And if that class is already loaded, then it does nothing. But in any case it doesn't create any objects.

And if you're a beginner in Java then understanding how it works internally isn't going to be helpful. All you need to know is that the method you asked about loads a class. You need to know what it means to "load" a class, of course, but you don't need to know what pointers are set and how memory is assigned or any of that.
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Howdy, Prabhat!

You know, one thing that might be helpful to you is the Reflection Tutorial. I think it will clear some of your doubts!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Thanks Roberto !

I agree with you but if question comes like how many ways to create new Objects, then three points are:

1) Using new operator
2) using clone() method
3) using Class.forName()

am i correct , please confirm !
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2996
    
    9
Prabhat Ranjan wrote:...if question comes like how many ways to create new Objects, then three points are:

1) Using new operator

Yes, always.
Prabhat Ranjan wrote:2) using clone() method

Yes, usually. Almost always. It's not strictly guaranteed to give a new object when it's called, but pretty much every library you will ever encounter probably does this.

Prabhat Ranjan wrote:3) using Class.forName()

No, never.

Prabhat Ranjan wrote:am i correct , please confirm !

Paul C already addressed this. Did you read his response?
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
yes i gone through his post.

if so ,then there is lot of confusion in the Java Discussion forum over internet that

there are 4 ways to create objects.

de-serilization is also one of them.

But now i understood there are actually 2 ways.1) Clone() and 2) New operator only.


Thanks for clarfication.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
if you go through this link over Class.forName() section:

http://download.oracle.com/javase/tutorial/reflect/class/classNew.html


If the fully-qualified name of a class is available, it is possible to get the corresponding Class using the static method Class.forName(). This cannot be used for primitive types. The syntax for names of array classes is described by Class.getName(). This syntax is applicable to references and primitive types.
Class c = Class.forName("com.duke.MyLocaleServiceProvider");

This statement will create a class from the given fully-qualified name.

it sould not be written here to create a class , instead it will here , load a class.

As you mentioned class.forName only load class.

am i right ?

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Prabhat Ranjan wrote:This statement will create a class from the given fully-qualified name.


Well champ, as Paul already mentioned, this will only cause the VM to load the corresponding class. Here's a sample of how you can create new objects using this:

Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
ok thanks means we can only load the Object into JVM using Class.forName()

but with the help of constructor instance , using your mentioned code we can create new object

as below code also:


here classforName is new Object of String class created using newInstance() ! right ?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2258
    
    3

Prabhat Ranjan wrote:ok thanks means we can only load the Object into JVM using Class.forName()


Well, I think a more correct statement would be "we can only cause the JVM to load a class and get the Class object using Class.forName()".

here classforName is new Object of String class created using newInstance() ! right ?


Yup!
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
Thanks Roberto for good Concept.

It was very good discussion with you in this topic.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Prabhat Ranjan wrote:I am closing this discussion.

Eh what? Anyone is free to ask more questions and/or discus it.

Anyway it's to difficult for Beginning Java so moving to Java in General


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Prabhat Ranjan
Ranch Hand

Joined: Oct 04, 2006
Posts: 391
ok you are right, sure some one can also add their views.
 
 
subject: How Class.ForName() actually creates new object internally passed in argument ?
 
Similar Threads
When to use new and newInstance() and what is the difference bet them.
What does Class.forName do?
Class.forName
Class.forName() question
Class.forName