Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax question

 
Tetsuo Suzuki
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Natesh Kurup
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • 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
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic