Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Syntax question

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly is meant by:

I know that {...} is for an array literal, so it makes sense that is an array of type Class. Is this an array of the Boolean wrapper class? On the 3rd line, where it references new Class[0], I also am not sure what is meant.
Javabeans blues...
Tetsuo!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm not sure how useful this would be, but the line:

creates a new array of Class objects (of length 0) and sets a reference to it in the variable parms.
Perhaps you could show a little more of the code this comes from so we can see the context it is being used in.
Corey
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an example of Java Reflection which enables you to discover at runtime the "content" of any class. The getMethod() method is invoked on a Class object and requires a method name and a list of arguments. The name and arguments univoquely make up the method signature. The first argument is the method name and the second a Class array containing the ordered types of the arguments, that is:
theSet = MyClass.class.getMethod("setVal",params);
theSet is a java.lang.reflect.Method object and represents the method in class MyClass having the following signature:
setVal(boolean b);
Now, setting params to a zero-length Class array, means that the next method you want to retrieve does not take any parameter, that is:
theGet = MyClass.class.getMethod("getVal",params);
theGet is a java.lang.reflect.Method object and represents the method in class MyClass having the following signature:
getVal();
The reason why we pass a zero-length array is because the Class.getMethod method requires a Class array to be passed. If that array does not contain any component, then it means that the method you want to retrieve does not have any paramaters.
Now that you have the Method objects you can invoke them. For instance,
MyClass m = new MyClass();
theGet.invoke(m,new Object[]{});
will invoke getVal() on the MyClass object m.
MyClass m = new MyClass();
theSet.invoke(m,new Object[]{new Boolean(true)});
will invoke setVal(true) on the MyClass object m.
[ April 03, 2002: Message edited by: Valentin Crettaz ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've worked with reflection before and, if I'm not mistaken, the method getMethod, which expects an array of Class objects, also accepts null as an empty parameter list. Therefore, doing this:

is roughly equivalent (functionally, at least) to this:

Corey
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

can i use reflection to invoke a method which has object parameter in my class.

ie; methodA(Object obj)

Thanks in advance

Natesh
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by NateshK Kurup:
can i use reflection to invoke a method which has object parameter in my class.

ie; methodA(Object obj)

Yes, you can specify any argument list you want. In this case, you'd use
 
reply
    Bookmark Topic Watch Topic
  • New Topic