• 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

How to access the method using the object

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}

}
}
*********************************************************************
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya manoj what you said is right.Lets wait anyone will help us
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ditmer
 
Manoj Paul
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

reply
    Bookmark Topic Watch Topic
  • New Topic