Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Setting Fields Information of a class using Reflection 
 

As in our previous example we have seen that we can get field values of a class by using the Field class. Now we can also set different field values of that class by using set() method.

 

Setting Fields Information of a class using Reflection

                         

As in our previous example we have seen that we can get field values of a class by using the Field class. Now we can also set different field values of that class by using set() method.

In our class SettingField we have created an object of "Rectangle" and we can get fields of that object by using getField() method and we can retrieve values of that fields by get() method. Now we can set fields value by using set() methods. 

 

 

 

 

 

    xfield.setInt(rect,new Integer(10));
    yfield.setInt(rect,new Integer(10));
    heightField.setInt(rect,new Integer(60));
    widthField.setInt(rect,new Integer(80));

Above lines of code sets x-axis, y-axis, height, width field values. Since we are setting integer values to its field so I have used setInt() method. Here is the example code for SettingField class :

SettingField.java

import java.awt.*;
import java.lang.reflect.*;

public class SettingField{
  public static void main(String[] argsthrows Exception{
    Rectangle rect=new Rectangle();
    Field xfield = rect.getClass().getField("x");
    Field yfield = rect.getClass().getField("y");
    Field heightField = rect.getClass().getField("height");
    Field widthField = rect.getClass().getField("width");
  System.out.println("---->> Before Setting Values <<----");
  System.out.println("X Value = "+ xfield.getInt(rect));
  System.out.println("Y Value = "+ yfield.getInt(rect));
  System.out.println("Height Value = "+ heightField.getInt(rect));
  System.out.println("Width Value = "+ widthField.getInt(rect));
    xfield.setInt(rect,new Integer(10));
    yfield.setInt(rect,new Integer(10));
    heightField.setInt(rect,new Integer(60));
    widthField.setInt(rect,new Integer(80));
  System.out.println("---->> After Setting Values <<----");
  System.out.println("X Value = "+ xfield.getInt(rect));
  System.out.println("Y Value = "+ yfield.getInt(rect));
  System.out.println("Height Value = "+ heightField.getInt(rect));
  System.out.println("Width Value = "+ widthField.getInt(rect));
   }
}

Output:

Download Source Code

                         

» View all related tutorials
Related Tags: java c ide class reference object io integer method get vi field using fields int this id ai create for

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.