In this tutorial you will learn about the Java Java Bean and its application with practical example.
Java Java Bean
Java Bean are classes that encapsulate many objects into a single object. It contains constructor, Getter, Setter Methods. With the help of Setter and Getter method user can set and get data. It also have zero argument constructor. Check out below code for more details.
Example: Let’s get understand it by an simple example.
#simpleBean.java: Create a java class for bean and do code as i did like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class simpleBean { private int id; private String name; public simpleBean() { } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } } |
#testBean.java: This will be another java file from which we will set and get data from our bean class. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class testBean { public static void main(String args[]) { simpleBean sb = new simpleBean();//object is created sb.setName("w3addaa");//setting value to the object System.out.println(sb.getName());//getting and printing the output } } |
Output: The output of above code will look like below image.
So this was the overview of bean class, we will use in next article of jsp:useBean action tag.