Java - Understanding beans

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

Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |