Bootstrap FreeKB - Java - Understanding beans
Java - Understanding beans

Updated:   |  Java articles

A bean is simply a class that adheres to the following rules.

  • Implements java.io.Serializable
  • A public no-argument constructor
  • Has getters and setters methods

Let's say you've created a class called Hello.java (in Eclipse in this example). 

 

This bean will print "John Doe".

package my.pkg;

public class Hello implements java.io.Serializable {
	private static final long serialVersionUID = -3774654564564563L;
	private String name;
	
    public void myName() {
    	name = "John Doe";
    }
	
    // getter
    public String getName() {
        return name;
    }
    
    // setter
    public void setName(String name) {
        this.name = name;
    }
}

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter d073d4 in the box below so that we can be sure you are a human.