java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.beans arrow How to get and set the Property of a Bean
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4084
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to get and set the Property of a Bean E-mail
User Rating: / 8
PoorBest 

This Java tip illustrates a method of getting and setting properties of a Bean. This tip uses expression and statement for setting the properties. Developer may get and set three types of properties, an Object, a primitive type, and an array. Both these classes use the name of the method that gets or sets the property.

    Object obj = new MyBean();
    try {
    
        // Developer may get the value of prop1
        Expression expr = new Expression(obj, "getProp1"
          new Object[0]);

        expr.execute();
        String s = (String)expr.getValue();
    
        // Developer may set the value of prop1
        Statement stmt = new Statement(obj, "setProp1"
          new Object[]{"new string"});

        stmt.execute();
    
        // Now developer may get the value of prop2
        expr = new Expression(obj, "getProp2"new Object[0]);
        expr.execute();
        int i = ((Integer)expr.getValue()).intValue();
    
        // Now developer may set the value of prop2
        stmt = new Statement(obj, "setProp2"
          new Object[]{new Integer(123)});
        stmt.execute();
    
        // Further get the value of prop1
        expr = new Expression(obj, "getProp3"new Object[0]);
        expr.execute();
        byte[] bytes = (byte[])expr.getValue();
    
        // Finally setting the value of prop1
        stmt = new Statement(obj, "setProp3"
          new Object[]{new byte[]{0x120x23}});
        stmt.execute();
        
    catch (Exception e) {
    }
    
    public class MyBean {
    
        // Property prop1
        String prop1;
        
        public String getProp1() {
            return prop1;
        }
        
        public void setProp1(String s) {
            prop1 = s;
        }
    
        // Property prop2
        int prop2;
        
        public int getProp2() {
            return prop2;
        }
        
        public void setProp2(int i) {
            prop2 = i;
        }
    
        // Property prop3
        byte[] prop3;
        
        public byte[] getProp3() {
            return prop3;
        }
        
        public void setProp3(byte[] bytes) {
            prop3 = bytes;
        }
    }


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.