A brief review of Java Classes with an Example

A Java program consists of one or more classes. Java represents, in a class, a group of objects of the same type. For example, a program is needed to generate five triangles. In this case, a class Triangle can be declared for being a blueprint of the five custom triangles.


A class can consist of the following components:

  • package declaration
  • package imports
  • a class name
  • zero or more properties (fields)
  • zero or more methods (a series of statements)


A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. They can be compiled separately and imported into other programs.

Access modifier

  • ( + ) public class or member : accessible anywhere
  • ( - ) private member : only accessible within the class
  • ( # ) protected member : accessible within the package or the subclasses/descendants
  • ( ~ ) (default) : accessible within the package (package mypkg; class MyClass{} )


Creating a custom object from a class is referred to as instantiation. In order to instantiate new instances from a class, the class must provide one or more constructors, whose name must be the same as the class name. A constructor is a special method that customizes a new instance by assigning specific values to some properties. The constructor method is always called with the keyword new together.


A geometric shape, triangle, is implemented in a Java class Triangle. Take a look at the source code below for the class Triangle to get an idea about how a class should be structured.

package mytools;

public class Triangle {

    private int a, b, c; // Three property members represent the length of three edges in a triangle, respectively

    public Triangle(int a, int b, int c) {
        // Constructor which initializes a, b and c
        this.a = a;
        this.b = b;
        this.c = c;

    public int getA() {// getter for a
        return a;

    public int getB() {// getter for b
        return b;

    public int getC() {// getter for c
        return c;

    boolean allPositive() {
        // Examine if all three lengths in a, b and c have a positive value
        // The method returns true if every length is positive and false otherwise
        return a > 0 && b > 0 && c > 0;

    public boolean isValid() {
        // Examine if a, b and c make a valid triangle        
        // The method returns true if a, b and c make a triangle and false otherwise. 
        return this.allPositive() && (a + b) > c && (b + c) > a && (c + a) > b;

Property members

Triangle class is in the package mytools. It has three private property members, a, b and c, for storing lengths of its three sides.

 private int a, b, c;

Method members

Three getters are provided for external access to these three private properties.

public int getA
public int getB
public int getC 

In addition, Triangle is able to perform two basic tasks, allPositive and isValid. Both tasks return a boolean.

boolean allPositive()
public boolean isValid()

Triangle.allPositive is accessible within the package mytools. Triangle.isValid is accessible globally.

Both methods utilize logical operations for validation. Triangle.allPositive evaluates a conditional expression which combines three conditions with two logical-AND (&&) operators. The method tells the caller if all sides has a positive length. Triangle.isValid evaluates a conditional expression which joins one method call and three conditions by using three logical-AND (&&) operators. The method tells the caller if three sides in a, b and c will make a triangle shape.

Last but not the least, in order to access all the members in a Java class, either user-defined or Java built-in, a new instance must be created for accessing services from a class.

Thus, Triangle needs a constructor to initialize new Triangle instances. A constructor has been defined in the following:

public Triangle(int a, int b, int c) {
    // Constructor which initializes a, b and c
    this.a = a;
    this.b = b;
    this.c = c;

The keyword this

In the constructor above, the keyword this is a reference to the current containing class object, which is Triangle. Prefixing this and a dot sign before the property name a,


will prevent the property a is shadowed by the constructor parameter a. By doing so, they can be distinguished from the other in the same scope to avoid ambiguity.

Mutable w/ setter methods

The current Triangle is immutable because it has no interface available for users to change its state i.e. the shape in this case, once it has been initialized e. Thus, a, b and c can be final.

 private final int a, b, c;

To make Triangle mutable, write the setter method for each property which requires updates according to the requirements. The setter method opens an interface through which a user can write a new value into the property. The typical format of a setter method is given below, based on the property a:

public void setA(int a) {
    this.a = a;

Instantiation w/ the keyword new

To access services from a Java class, an instance object must be instantiated.

The following post has a discussion about instantiating new instances.

Java Review: Instantiation w/ the keyword new


Write a Java class Person.

As Person has been designed in a specific way so that the following requirements should be met:

  • It must be public and have the following private property members: firstName, lastName, gender, email, bDate
  • Person must have a constructor that initializes lastName, firstName and gender for each new Person instance.
  • Person allows outsiders to read its property members.