aspose file tools*
The moose likes Java in General and the fly likes Class instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class instantiation" Watch "Class instantiation" New topic
Author

Class instantiation

Shilpa Tonk
Greenhorn

Joined: Mar 15, 2010
Posts: 6
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

Joined: Dec 20, 2006
Posts: 2503
    
    8

Moved to our Java in General forum.


OCUP UML fundamental and ITIL foundation
youtube channel
Shilpa Tonk
Greenhorn

Joined: Mar 15, 2010
Posts: 6
Jan Cumps wrote:Moved to our Java in General forum.


I am not getting.Please help me out.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class instantiation