gudii9
asked on
reflection example
Hi,
I am trying below example
http://examples.javacodegeeks.com/core-java/reflection/java-reflection-example/
i got below output
i was not clear on how i got below output
One private Fieldname is: type
what is 'rent' here. How 'type' was printed as filedValue. please advise
I am trying below example
http://examples.javacodegeeks.com/core-java/reflection/java-reflection-example/
package com.javacodegeeks.core.reflection;
import java.lang.reflect.*;
import java.util.Arrays;
public class ReflectionExample {
public static void main(String[] args) {
// Obtain the class object if we know the name of the class
Class rental = RentCar.class;
try {
// get the absolute name of the class
String rentalClassPackage = rental.getName();
System.out.println("Class Name is: " + rentalClassPackage);
// get the simple name of the class (without package info)
String rentalClassNoPackage = rental.getSimpleName();
System.out.println("Class Name without package is: "
+ rentalClassNoPackage);
// get the package name of the class
Package rentalPackage = rental.getPackage();
System.out.println("Package Name is: " + rentalPackage);
// get all the constructors of the class
Constructor[] constructors = rental.getConstructors();
System.out.println("Constructors are: "
+ Arrays.toString(constructors));
// get constructor with specific argument
Constructor constructor = rental.getConstructor(Integer.TYPE);
// initializing an object of the RentCar class
RentCar rent = (RentCar) constructor.newInstance(455);
// get all methods of the class including declared methods of
// superclasses
// in that case, superclass of RentCar is the class java.lang.Object
Method[] allmethods = rental.getMethods();
System.out.println("Methods are: " + Arrays.toString(allmethods));
for (Method method : allmethods) {
System.out.println("method = " + method.getName());
}
// get all methods declared in the class
// but excludes inherited methods.
Method[] declaredMethods = rental.getDeclaredMethods();
System.out.println("Declared Methods are: "
+ Arrays.toString(declaredMethods));
for (Method dmethod : declaredMethods) {
System.out.println("method = " + dmethod.getName());
}
// get method with specific name and parameters
Method oneMethod = rental.getMethod("computeRentalCost",
new Class[] { Integer.TYPE });
System.out.println("Method is: " + oneMethod);
// call computeRentalCost method with parameter int
oneMethod.invoke(rent, 4);
// get all the parameters of computeRentalCost
Class[] parameterTypes = oneMethod.getParameterTypes();
System.out.println("Parameter types of computeRentalCost() are: "
+ Arrays.toString(parameterTypes));
// get the return type of computeRentalCost
Class returnType = oneMethod.getReturnType();
System.out.println("Return type is: " + returnType);
// gets all the public member fields of the class RentCar
Field[] fields = rental.getFields();
System.out.println("Public Fields are: ");
for (Field oneField : fields) {
// get public field name
Field field = rental.getField(oneField.getName());
String fieldname = field.getName();
System.out.println("Fieldname is: " + fieldname);
// get public field type
Object fieldType = field.getType();
System.out.println("Type of field " + fieldname + " is: "
+ fieldType);
// get public field value
Object value = field.get(rent);
System.out.println("Value of field " + fieldname + " is: "
+ value);
}
// How to access private member fields of the class
// getDeclaredField() returns the private field
Field privateField = RentCar.class.getDeclaredField("type");
String name = privateField.getName();
System.out.println("One private Fieldname is: " + name);
// makes this private field instance accessible
// for reflection use only, not normal code
privateField.setAccessible(true);
// get the value of this private field
String fieldValue = (String) privateField.get(rent);
System.out.println("fieldValue = " + fieldValue);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
package com.javacodegeeks.core.reflection;
public class RentCar {
private int rate;
private String type;
public int price;
public RentCar(int length) {
if (length < 455) {
type = "small";
rate = 35;
} else if ((length >= 455) && (length < 495)) {
type = "mid-sized";
rate = 45;
} else if (length >= 495) {
type = "large";
rate = 55;
}
}
public int getRate() {
return rate;
}
public String getType() {
return type;
}
public void setRate(int rate) {
this.rate = rate;
}
public void setType(String type) {
this.type = type;
}
public void computeRentalCost(int numDays) {
price = numDays * rate;
System.out
.println("The cost of your rental car is " + price + " euros");
}
}
i got below output
Class Name is: com.javacodegeeks.core.reflection.Re ntCar
Class Name without package is: RentCar
Package Name is: package com.javacodegeeks.core.reflection
Constructors are: [public com.javacodegeeks.core.reflection.Re ntCar(int) ]
Methods are: [public boolean java.lang.Object.equals(java.lang.Ob ject), public native int java.lang.Object.hashCode( ), public java.lang.String java.lang.Object.toString( ), public int com.javacodegeeks.core.ref lection.Re ntCar.getR ate(), public java.lang.String com.javacodegeeks.core.ref lection.Re ntCar.getT ype(), public void com.javacodegeeks.core.ref lection.Re ntCar.setR ate(int), public void com.javacodegeeks.core.ref lection.Re ntCar.setT ype(java.l ang.String ), public void com.javacodegeeks.core.ref lection.Re ntCar.comp uteRentalC ost(int), public final native java.lang.Class java.lang.Object.getClass( ), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll (), public final void java.lang.Object.wait() throws java.lang.InterruptedExcep tion, public final void java.lang.Object.wait(long ) throws java.lang.InterruptedExcep tion, public final native void java.lang.Object.wait(long ,int) throws java.lang.InterruptedExcep tion]
method = equals
method = hashCode
method = toString
method = getRate
method = getType
method = setRate
method = setType
method = computeRentalCost
method = getClass
method = notify
method = notifyAll
method = wait
method = wait
method = wait
Declared Methods are: [public int com.javacodegeeks.core.reflection.Re ntCar.getR ate(), public java.lang.String com.javacodegeeks.core.ref lection.Re ntCar.getT ype(), public void com.javacodegeeks.core.ref lection.Re ntCar.setR ate(int), public void com.javacodegeeks.core.ref lection.Re ntCar.setT ype(java.l ang.String ), public void com.javacodegeeks.core.ref lection.Re ntCar.comp uteRentalC ost(int)]
method = getRate
method = getType
method = setRate
method = setType
method = computeRentalCost
Method is: public void com.javacodegeeks.core.reflection.Re ntCar.comp uteRentalC ost(int)
The cost of your rental car is 180 euros
Parameter types of computeRentalCost() are: [int]
Return type is: void
Public Fields are:
Fieldname is: price
Type of field price is: int
Value of field price is: 180
One private Fieldname is: type
fieldValue = mid-sized
i was not clear on how i got below output
One private Fieldname is: type
// How to access private member fields of the class
// getDeclaredField() returns the private field
Field privateField = RentCar.class.getDeclaredField("type ");
String name = privateField.getName();
System.out.println("One private Fieldname is: " + name);
// makes this private field instance accessible
// for reflection use only, not normal code
privateField.setAccessible(true);
// get the value of this private field
String fieldValue = (String) privateField.get(rent);
System.out.println("fieldValue = " + fieldValue);
what is 'rent' here. How 'type' was printed as filedValue. please advise
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER