Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21297
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roberto for good Concept.

It was very good discussion with you in this topic.

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you are right, sure some one can also add their views.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic