• 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

is it possible to crete an instance without new operator? this is the smaple code i tried which dint

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinod,



how do you know it throws error ?



All empty catch statements. Do you think you caught the error?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is it complie error ?
 
Sheriff
Posts: 22780
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
Object does not have a printit() method. You need to cast the reference first to something that does have this method (like trialVinod).
 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Rob. But, i dont understand why we have to do it. Because, i am creating an object out of the class on which that method belongs. So, why we need to type cast??


yes... it was an compiler error.
 
Rob Spoor
Sheriff
Posts: 22780
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
It's the same as the following: Your source discards the fact that the object is actually a trialVinod object. The only way to be able to use the object as a trialVinod object again is by casting.

With Class.forName("trialVinod").newInstance(), the compiler does not even know the class, nor can it. It cannot convert the String into a Class object (the JVM can, but the compiler can't). Therefore, it doesn't know that the Class is a Class<trialVinod>.
This is often the case with Class.forName(). In fact, the class whose name is given to Class.forName does not even have to be available at compile time. This is a technique often used with plugins. Interfaces or base classes can solve these "method not found" problems mostly.

The following little example will create a user-provided set instance and print it:
 
author
Posts: 23949
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your source discards the fact that the object is actually a trialVinod object. The only way to be able to use the object as a trialVinod object again is by casting.



What Rob said is 100% correct -- you should know something about the object that is being instantiated (maybe an interface that it supports). And obviously, you can't cast it, because at compile time, you don't know anything about it.


However, what if you use the newInstance() method because you don't know about the class at compile time? And it doesn't support an interface that is usable?

So... if you don't know about the class at compile time, then yes, you need to use the newInstance() method to get an instance at runtime. And to call methods on it at runtime, you need to use the getMethod() (or getMethods() for all of them) to get the method that you want, and then use the invoke() method using the correct Method object.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic