This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
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
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 109
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: 109
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: 35443
    
    9
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:


Android appsImageJ pluginsJava web charts
Ganesh Kumar
Ranch Hand

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

Joined: Jan 18, 2007
Posts: 109
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to access the method using the object
 
Similar Threads
protected access when mixing references and objects
RMI UnmarshalException
Implementing a simple Object Pool
ToString method
To map backing bean from a selected drop-down value