• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class instantiation

 
Shilpa Tonk
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
can anyone tell me the difference between these two lines:


1-Class.forName("A.java").newInstance();

2-A a= new A();
 
Jan Cumps
Bartender
Posts: 2576
11
C++ Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to our Java in General forum.
 
Shilpa Tonk
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:Moved to our Java in General forum.


I am not getting.Please help me out.
 
Rob Spoor
Sheriff
Pie
Posts: 20372
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan just moved your thread to a different forum. It wasn't an answer.

Both achieve exactly the same. Well, after you remove the ".java" from the Class.forName call. Remember, you must specify full class names, not file names.
The difference is that with Class.forName you can use a class name that may not be known or available when you compile the class, whereas in the second you really hard code the class.

An example:
This list class may even be provided to you at some later time after you compiled your class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic