• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

newInstance() doubt

 
Greenhorn
Posts: 12
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anybody can make me understand why is not running with even obj is object class.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why in above code is not giving any output!!!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79179
377
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK Campbell mere bhai!!!
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All posts in English please
 
Ranch Hand
Posts: 83
Netbeans IDE MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is my first ans in any forum
This is the code dude enjoy
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic