Definition of "JavaBeans", "public" and "private".

In this tutorial we are going to define the words "public", "private" and we are going to talk about the "javabean" concept.

Until now our variables were "public" and we could get to them from the outside of their classes. In this example we can see that when we change the variable "name" from "public" to "private", we can´t enter the variable from the Class "Tutorial12" using the same system we used until now.

package com.edu4java.tutorial12;

/**
 * A class is a mould from which we can create objects or instances
 * An object is an instance of a class
 * 
 * JavaBeans are reusable software components for Java
 * that can be manipulated visually in a builder tool.
 */
public class Dog {
	// instance variables

	private String name;
	public double weight;
	public String colour;

	public void printToConsole() {
		System.out.println();
		System.out.println("name: " + this.name);
		System.out.println("colour: " + this.colour);
		System.out.println("weight: " + this.weight);

	}
}

package com.edu4java.tutorial12;

public class Tutorial12 {

	public static void main(String[] args) {
		Dog dog1 = new Dog();
		dog1.name = "Coco";
		dog1.colour = "brown";
		dog1.weight = 1.5;
		dog1.printToConsole();

		Dog dog2 = new Dog();
		dog2.name = "Rope";
		dog2.colour = "green";
		dog2.weight = 50;
		dog2.printToConsole();

//		printToConsole(dog1);
//		printToConsole(dog2);
	}

	private static void printToConsole(Dog dog) {
		System.out.println();
		System.out.println("name: " + dog.name);
		System.out.println("colour: " + dog.colour);
		System.out.println("weight: " + dog.weight);

	}

}

Connected to the data access, we have the JavaBean concept. According to the Sun Microsystems standards, JavaBeans are defined as "reusable software components for Java that can be manipulated visually in a builder tool".

In order to function as a JavaBean class, an object class must obey certain conventions about method naming, construction, and behaviour. One of these required conventions is that the class properties must be accessible using get and set methods, according to a standard naming convention.

Below we can see the "Dog" class working as a JavaBean and the class "Tutorial12" using the getters and setters methods to access the properties according to JavaBean standards.

package com.edu4java.tutorial12;

/**
 * A class is a mould from which we can create objects or instances
 * An object is an instance of a class
 * 
 * JavaBeans are reusable software components for Java
 * that can be manipulated visually in a builder tool.
 */
public class Dog {
	// instance variables

	private String name;
	private double weight;
	private String colour;
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public String getColour() {
		return colour;
	}

	public void setColour(String colour) {
		this.colour = colour;
	}

	public void printToConsole() {
		System.out.println();
		System.out.println("name: " + this.name);
		System.out.println("colour: " + this.colour);
		System.out.println("weight: " + this.weight);

	}
}

package com.edu4java.tutorial12;

public class Tutorial12 {

	public static void main(String[] args) {
		Dog dog1 = new Dog();
		dog1.setName("Coco");
		dog1.setColour("brown");
		dog1.setWeight(1.5);
		dog1.printToConsole();

		Dog dog2 = new Dog();
		dog2.setName("Rope");
		dog2.setColour("green");
		dog2.setWeight(50);
		dog2.printToConsole();

//		printToConsole(dog1);
//		printToConsole(dog2);
	}

	private static void printToConsole(Dog dog) {
		System.out.println();
		System.out.println("name: " + dog.getName());
		System.out.println("colour: " + dog.getColour());
		System.out.println("weight: " + dog.getWeight());

	}

}

<< Previous Next >>