aspose file tools*
The moose likes Java in General and the fly likes Reflection Usage Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Reflection Usage Question" Watch "Reflection Usage Question" New topic
Author

Reflection Usage Question

Chiranjivi Vijay
Greenhorn

Joined: Jan 15, 2012
Posts: 5
Hi Everyone

I have a question in Java reflection usage. Could anyone help me out how we can access the variables map1, map2 using reflection from the following code




I tried the reflection technique by doing the following




When I tried the above code and debugged the code in eclipse I could see that the Field contain the necessary data but I couldn't retrieve the value from the Field object as my instance refers to Manager for the the get method I believe. Can anyone tell me what exactly has to be done to get the value for map1 and map2.

Thanks
Vijay
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Chiranjivi Vijay wrote: I couldn't retrieve the value from the Field object as my instance refers to Manager for the the get method I believe.


Could you explain what do you actually want to retrieve ?
A class doesn't have any fields (unless fields declared as static), only objects (instances) of this class have non-static fields.
Map1 and map2 are not static, then they don't exists within the class, they only exist in objects of this class,
but your code is trying to access these fields in the class, not the object.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You need an instance of class Lead. Line 13 uses an instance of class Manager, line 14 an instance of class Class. Those two instances do not have the field you're looking for.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Chiranjivi Vijay
Greenhorn

Joined: Jan 15, 2012
Posts: 5
Hi Ireneusz

I understand that the variables declared are not static. I was wondering how to use reflection technique for accessing the variables map1 and map2 declared in the inner class Lead. I am not sure how to get those fields. I have tried based on the code what I have mentioned before.

Thanks
Vijay

Chiranjivi Vijay
Greenhorn

Joined: Jan 15, 2012
Posts: 5
Hi Rob

I got what you meant but my concern here is since the inner class Lead is private static how can I pass the instance of that class which I can't access from outside. Could you please help me out how to access the variable map1 and map2 by passing the instance of class Lead?

Thanks
Vijay
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Chiranjivi Vijay wrote:Hi Ireneusz

I understand that the variables declared are not static. I was wondering how to use reflection technique for accessing the variables map1 and map2 declared in the inner class Lead. I am not sure how to get those fields. I have tried based on the code what I have mentioned before.


Hello,

here is an example:


Chiranjivi Vijay
Greenhorn

Joined: Jan 15, 2012
Posts: 5
Hi Ireneusz

Thank you for your instant reply.

Based on what you said



I believe its exactly the same in my code(second way of accessing) as well right. But here I am getting an exception. and couldn't retrieve the data.

I will explain one possible scenario and please let me know whether we could use reflection or not.

Say the Manager class has been loaded into the JVM and our Test program class has to access it. Here, all we have is instance of Manager class and in this situation how can we access the private variables map1 and map2 from Lead class. Which means we don't have access to create a new instance of Lead class because if we do then the values that are present already in map1 and map2 will be lost as we will be getting a new map1 and map2 instances causing loss of data.

Please let me know if it is not clear. I will try to explain in detail.

Thanks
Vijay


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Chiranjivi Vijay wrote:But here I am getting an exception. and couldn't retrieve the data.


ItDoesntWorkIsUseless. You need to TellTheDetails. Copy/paste the exact, complete error message, including the stack trace. Also, you'll get better help if, instead of posting non-working snippets, you provide an SSCCE that others can compile and run without change, and that has everything needed to demonstrate your problem and nothing else.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reflection Usage Question
 
Similar Threads
Problem regarding HashMap.
HashMap Problem
HashMap values-- I think I know why but how do I fix this null pointer exception?
java.lang.ClassCastException: java.lang.String cannot be cast to mycom.lang.MyString
determine the input type of the value of the function parameter map