aspose file tools*
The moose likes Beginning Java and the fly likes How to access the method using the object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to access the method using the object" Watch "How to access the method using the object" New topic
Author

How to access the method using the object

Manoj Paul
Ranch Hand

Joined: Jan 18, 2007
Posts: 110
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

Joined: Jul 02, 2007
Posts: 113
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

Joined: Jan 18, 2007
Posts: 110
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

Joined: Jul 02, 2007
Posts: 113
Ya manoj what you said is right.Lets wait anyone will help us
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39576
    
  27
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:


Ping & DNS - updated with new look and Ping home screen widget
Ganesh Kumar
Ranch Hand

Joined: Jul 02, 2007
Posts: 113
Thanks Ditmer
Manoj Paul
Ranch Hand

Joined: Jan 18, 2007
Posts: 110
Thank you Ulf, your suggestion was very helpful. Now I am able to compile and run the program efficiently. Thanks once again.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to access the method using the object
 
Similar Threads
protected access when mixing references and objects
ToString method
RMI UnmarshalException
Implementing a simple Object Pool
To map backing bean from a selected drop-down value