File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Syntax question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Syntax question" Watch "Syntax question" New topic
Author

Syntax question

Tetsuo Suzuki
Greenhorn

Joined: Feb 21, 2002
Posts: 22
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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Nov 23, 2004
Posts: 19
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

Joined: Aug 07, 2003
Posts: 1646
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Syntax question
 
Similar Threads
Annotations and reuse
Reflection : Using getDeclaredMethod without method arguments
Convert String Array to Byte Array
How to check how many array elements are set in function
How to copy java Object Array into javascript Array (of Objects)