File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes reflection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "reflection" Watch "reflection" New topic


anita mitra

Joined: Jul 03, 2009
Posts: 21

suppose i have a stubclass object... i am trying to get the details from stubclss using reflection concept

this is the code

and getCost method is

i couldn't understand these lines-
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14910

Let's look at those three lines:

1. Declares an array of Class objects and initializes it with two elements, which are both String.class (the Class object of class String).

2. Uses the reflection API to find a method named "getCost" which takes two strings as arguments. In other words, the class that "myclass" refers to must have a method that looks like this:

public ... getCost(String arg1, String arg2)

(the return type is not specified).

3. Creates a new Object array with two strings, with the values "chennai" and "goa".

Note: It is never necessary to create a new String object from a literal like is done in line 3. This should have been written like this:

Object[] args = new Object[] { "chennai", "goa" };

Shorter, easier to read and doesn't create unnecessary String objects.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
anita mitra

Joined: Jul 03, 2009
Posts: 21

thanks a lot .. got it..
It is sorta covered in the JavaRanch Style Guide.
subject: reflection
It's not a secret anymore!