File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to access the method using the object

 
Manoj Paul
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends...
Can any one tell me what should I do to access the watch() method using the object obj in the following program.

Your suggestion are highly welcomed.

***********************************************************************
public class Manoj
{
String you="YOU";


Manoj()
{
System.out.println("In const");
}
public void watch()
{
System.out.println("In Watch");
}

public static void main(String a[]) throws
InstantiationException,IllegalAccessException
{
try
{
Object obj = Class.forName("Manoj").newInstance();
//obj.watch();
//System.out.println("Your Entered String: "+obj.you );

}
catch(Exception e)
{
System.out.println("Ex: "+e);
}

}
}
*********************************************************************
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi manoj,

" Object obj = Class.forName("Manoj").newInstance();" Their is a problem with this line i suppose.If we create a instance by new its working instead of the above.

Ganesh Kumar
 
Manoj Paul
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But Ganesh,
I don't want to use the new operator here, because we can create an object without using new operator too. Run the program, you can see that an object is already created, because the constructor is called in the process. But when i tried to access the method watch() with the help of the object obj, its not working, thats what i want to know and what should i do to access the method.

But I am yet to get any help.
Thank you

 
Ganesh Kumar
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya manoj what you said is right.Lets wait anyone will help us
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you know what kind of object "obj" is, you can cast it:



You'll need to catch ClassCastException as well.


An interesting twist would be to use an interface here:


The you can instantiate all kinds of classes, and don't have to know which one it is in particular:
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ditmer
 
Manoj Paul
Ranch Hand
Posts: 112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ulf, your suggestion was very helpful. Now I am able to compile and run the program efficiently. Thanks once again.

 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic