Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: java Reflection Question - I am lost.

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking java Reflection Question - I am lost.

    Please compile and run this example. Please read step by step comments to understand what I expect to achieve from this sample program.
    import java.util.*;
    import java.lang.reflect.*;
     
    //I first create a simple local class
    class MyClass1
    {	
      public Double aDouble1;
      public Double aDouble2;
     
      public MyClass1()
      {
    	  aDouble1 = 250.0;
    	  aDouble2 = -100.9;
      }
    }  
     
    public class Identifier
    {
      public static void main(String [] args)
      {
    	  //OK. I create an instance of MyClass1
    	  MyClass1 m1 = new MyClass1();
     
    	  //Then I get the runtime Class of that object
    	  Class<?> that = m1.getClass();
     
     
    	  //Then I get all the declared fields of that object
    	  Field [] fields = that.getDeclaredFields();
     
    	  //I define an Object array to the size of Field array
    	  Object[] fieldsAsObjects = new Object[fields.length];
     
    	  //Now I get each Field object into the Object array
    	  try
    	  {  
    	    for (int next = 0; next < fields.length; next++)
    	    {
    		  fieldsAsObjects[next] =  fields[next].get(m1);
    	    }
    	  }
    	  catch(Exception exp)
    	  {
    		  exp.printStackTrace();
    	  }
     
     
    	  //Since now I have fields of m1 object in the array, I go change
    	  //then to a new value (or rather make them zeros).
    	  for (int next = 0; next < fieldsAsObjects.length; next++)
    	    {
    		  fieldsAsObjects[next] =  0.0;
    	    }
     
    	  //Afterwards I EXPECT THE m1.aDouble1 and m1.aDouble2 to be changed
    	  //to 0.0. But it is not happpening. Why and how to?
    	  System.out.println(m1.aDouble1);
    	  System.out.println(m1.aDouble2);
     
      }
    }
    Last edited by Json; May 13th, 2010 at 02:14 PM. Reason: Please use code tags.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java Reflection Question - I am lost.

    Hello there,

    I tried running your code and this is what I've come up with, you cant really just grab the objects and change them like that. Even though it returns an object, its actually a FieldAccessor, I dont really know how this works deep down but you just cant do it that way. Luckily though there is another way of changing the field value.

    See below.

    import java.lang.reflect.Field;
     
    //I first create a simple local class
    class MyClass1 {
        public Double aDouble1;
        public Double aDouble2;
     
        public MyClass1() {
            this.aDouble1 = 250.0;
            this.aDouble2 = -100.9;
        }
    }
     
    public class ReflectionTest1 {
     
        public static void main(final String[] args) {
            // OK. I create an instance of MyClass1
            final MyClass1 m1 = new MyClass1();
     
            // Then I get the runtime Class of that object
            final Class<?> that = m1.getClass();
     
            // Then I get all the declared fields of that object
            final Field[] fields = that.getDeclaredFields();
     
            // Loop through the fields and call the set method to change it and
            // pass in the object on which you want to alter the field on
            try {
                for (int next = 0; next < fields.length; next++) {
                    fields[next].set(m1, 0.0);
                }
            } catch (final Exception exp) {
                exp.printStackTrace();
            }
     
            // Afterwards I EXPECT THE m1.aDouble1 and m1.aDouble2 to be changed
            // to 0.0. But it is not happpening. Why and how to?
            System.out.println(m1.aDouble1);
            System.out.println(m1.aDouble2);
     
        }
    }

    As you can see here, I use the field.set() method to set a new value on that field on object m1. Hope that makes any sense.

    // Json

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java Reflection Question - I am lost.

    Hi Json,

    Thanks. And surely it makes sense and cute way.

    Thanks again!

    prain

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: java Reflection Question - I am lost.

    No problem. Lets get this thread marked as solved then

    // Json

Similar Threads

  1. lost
    By nyny in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2010, 07:32 AM
  2. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM
  3. Need Help! I'm completly lost!
    By DeMolay8613 in forum Java Theory & Questions
    Replies: 0
    Last Post: January 11th, 2010, 01:21 PM
  4. please could you help me with this java problem im very lost
    By eyeore in forum Java Theory & Questions
    Replies: 4
    Last Post: September 7th, 2009, 09:19 AM
  5. running a class from another projects, reflection
    By led1433 in forum Object Oriented Programming
    Replies: 7
    Last Post: July 29th, 2009, 01:47 PM