• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newInstance() doubt

 
Ranjeet Kr Rai
Greenhorn
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Anybody can make me understand why is not running with even obj is object class.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Ranjeet Kr Rai
Greenhorn
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why in above code is not giving any output!!!
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranjeet Kr Rai
Greenhorn
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please you re-write the above code with java.lang.reflect.Constructor so that the above program will give output
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK Campbell mere bhai!!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All posts in English please
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my first ans in any forum
This is the code dude enjoy
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that example still requires AClass to be known at compile time, for the cast and reference variable.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic