• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • 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 Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is it complie error ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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:
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!