This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 14074
    
  16

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: 19655
    
  18

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: 38045
    
  22
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: 38045
    
  22
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: 19655
    
  18

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: 4344
    
    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: 19655
    
  18

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.
 
jQuery in Action, 2nd edition
 
subject: newInstance() doubt
 
Similar Threads
Overriding and Overloading
interfaces used to work on my system but now they dont :)
Abstract class??
why Throws to main method
Overriding and Overloading.