0% found this document useful (0 votes)
35 views2 pages

How To Create A Java Bean

To create a Java bean, one must: 1) Define a class with private member variables and public getter and setter methods for each variable along with a no-argument constructor. 2) Save the class as a .java file. 3) Create another class that instantiates the Java bean and calls its getter and setter methods. 4) Compile and run the program to test that the Java bean's properties can be accessed as intended.

Uploaded by

nandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

How To Create A Java Bean

To create a Java bean, one must: 1) Define a class with private member variables and public getter and setter methods for each variable along with a no-argument constructor. 2) Save the class as a .java file. 3) Create another class that instantiates the Java bean and calls its getter and setter methods. 4) Compile and run the program to test that the Java bean's properties can be accessed as intended.

Uploaded by

nandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

How to Create a Java Bean

A Java bean is a Java class that has private member variables, public getter and setter
methods, and a zero-argument, public constructor (supplied automatically by the
compiler). To create a Java bean, follow these seven steps.

1. Open your text editor and create a new file that will contain the Java bean source.
Type in the following Java statements:

The Java bean has two properties, firstName and lastName. A property is a private


variable exposed to external programs by means of getter and setter methods.

2. Save your file as Person.java.


3. Open your text editor to create the class that will instantiate the Java bean. Type in
the following Java statements:
The program will instantiate the Java bean and then call the setter and getter
methods of the newly created Java bean.

4. Save your file as CreateAJavaBean.java.


5. Open a command prompt and navigate to the directory containing your new Java
programs. Then type in the command to compile the Java bean source and hit Enter.

6. Type in the command to compile the Java application to instantiate the Java bean
and hit Enter.
7. You are ready to test your Java program. Type in the command to run the Java
runtime launcher and hit Enter. Observe the data from your Java bean.

You might also like