• 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

Acessing a class method

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Let an example be like this:
>
> ******************************************************
> ***********************
>

> public class a {
> int i=10;
> public void showI()
> {
> System.out.println("Value of i = "+i)
}
> public static void main(String a[]) throws
> InstantiationException,IllegalAccessException {
> Class clazz = Object.class;
> Object oo = clazz.newInstance();
> System.out.println(oo);
> }
> }
>


> **************************************************
>
> How can i access the methow showI() using the object
> oo.
> Please tell me.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't. oo is an instance of java.lang.Object.

Perhaps you can explain more about what you really want to achieve?
 
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
Well i want to create an object of the class without using the new operator so that to access the showI() method.

Can you help me out?
please advice.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd use the reflection API: First, use Class.getMethod to retrieve a Method objects for "showI", then use its invoke method to call the actual method with the object oo as parameter.

Make sure "oo" is a reference to an object of type "a", not of type "Object".

It would be helpful to read the javadocs of the Class class (and the associated classes Field and Method), so that you get a feeling for all the things the reflection API can do.
[ July 03, 2007: Message edited by: Ulf Dittmer ]
 
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 Sir,
But I dont know how will i apply this in my mentioned program...
Can you help me please?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's not much to help with: create an instance of "a" (you already have that - almost), use the getMethod method of the Class object for class "a" to obtain a Method object for the showI method, and then call the "invoke" method of that Method object to actually run the method.

Which of these steps are you having difficulty with? Did you read the javadocs?
[ July 03, 2007: Message edited by: Ulf Dittmer ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic