aspose file tools*
The moose likes Beginning Java and the fly likes newInstance() doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "newInstance() doubt" Watch "newInstance() doubt" New topic
Author

newInstance() doubt

Ranjeet Kr Rai
Greenhorn

Joined: Feb 05, 2011
Posts: 12


Anybody can make me understand why is not running with even obj is object class.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14144
    
  18

What does "is not running" mean?

Don't eat up the exceptions like you are doing here:

Because then you will never know if an exception happened. At least print some information when an exception happens:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ranjeet Kr Rai
Greenhorn

Joined: Feb 05, 2011
Posts: 12

i am asking about and i have taken exceptions there because it needed there to compile and run not to check the exception messages
Ranjeet Kr Rai
Greenhorn

Joined: Feb 05, 2011
Posts: 12

why in above code is not giving any output!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

PatienceIsAVirtue.

And if you would have listened to Jesper you would have seen an InstantiationException occurred on line 13 when you tried to create the instance. The reason is very clear: newInstance() tries to invoke the constructor that has no arguments. However, your class does not have such a constructor. The only constructor requires a String. That means that you must use java.lang.reflect.Constructor to create the instance.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ranjeet Kr Rai
Greenhorn

Joined: Feb 05, 2011
Posts: 12

please you re-write the above code with java.lang.reflect.Constructor so that the above program will give output
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
Ranjeet Kr Rai wrote:please you re-write the above code with java.lang.reflect.Constructor so that the above program will give output
No. Have a look in the Java™ Tutorials about reflection.
Ranjeet Kr Rai
Greenhorn

Joined: Feb 05, 2011
Posts: 12

OK Campbell mere bhai!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38765
    
  23
All posts in English please
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81



This is my first ans in any forum
This is the code dude enjoy
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

That looks good, except you've started using AClass.class to find the class. That makes the point of using reflection useless; if you already know the class is going to be AClass, why not just use new AClass("hello")?

The following on the other hand makes much more sense (assuming that the "AClass" actually comes from somewhere else):
Note that this is a direct combination of two lines of code you posted yourself. All I did was replace AClass.class by Class.forName("AClass"), taken from your original post.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4385
    
    8

Except that example still requires AClass to be known at compile time, for the cast and reference variable.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

D'oh! I completely missed that. Perhaps this example isn't the best for reflection; sure it works, but it's useless. Reflection mostly is used when the variable's reference type is either an interface or an abstract class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newInstance() doubt