JavaObject-oriented programmingClasses and objects

Defining classes

10 seconds read

Declaring new classes

When programmers write an object-oriented program they use standard classes as building blocks. But they often need to declare new program-specific classes.

In order to declare a new class, you should write the class keyword and a name following it. Let's declare a class named Nothing:

class Nothing {
    // empty body
}

A class body can include fields, methods and constructors. Fields are variables storing data, methods defines a behaviour and constructors are special kinds of methods allowing us to create and initialize new objects of the class.

Fields and methods are considered as class members.

Not all Java classes have fields and methods. Sometimes you will see classes that don't contain fields or methods.

The source code of a class is placed in .java file. Usually, a source code file contains only a single class and has the same name as the class. But sometimes, a file can contain two or more classes.

Writing fields

A field is a variable that stores data. It may have any type, including primitive types (int, float, boolean and so on) and classes (and, even, the same class). A class can have as many fields as you need.

Let's declare a class for representing a patient in a hospital information system.

/**
 * The class is a "blueprint" patients
 */
class Patient {

    String name;
    int age;
    float height;
}

The class represents a patient of a hospital. It has three fields for storing an important information: name, age and height. Each object of the class has the same fields but each own values of these fields.

Creating objects

Let's create an instance of the class Patient using the new:

Patient patient = new Patient();

When you create an object, each field is initialized with the default value of the corresponding type.

System.out.println(patient.name); // it prints null
System.out.println(patient.age); // it prints 0

Creating multiple objects of the same class

The following program creates two objects of the class Patient. Each object outputs its name invoking the method say.
Note, both classes are placed in the same file named PatientDemo.class.

public class PatientDemo {
    
    public static void main(String args[]) {
        
        Patient john = new Patient();
        
        john.name = "John";
        john.age = 30;
        john.height = 180;
        
        System.out.println(john.name + " " + john.age + " " + john.height);
            
        Patient alice = new Patient();

        alice.name = "Alice";
        alice.age = 22;
        alice.height = 165;
        
        System.out.println(alice.name + " " + alice.age + " " + alice.height);
    }
}

class Patient {

    String name;
    int age;
    float height;
}

The output is:

John 30 180
Alice 22 165

So, it's possible to define a new simple class with several fields. Then you can create objects of the class. Fields keep the current state (data) of the objects.

How did you like the theory?
Report a typo