English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)

Java Reader/Writer

Other Java topics

Java Classes and Objects

In this tutorial, you will learn object-oriented programming in Java and learn about Java classes and objects with the help of examples.

Java is an object-oriented programming language. It is based on the concept of objects.

These objects have two features:

  • Status (field)

  • Behavior (method)

For example,

  1. The light is an object
    Status: Bright or not bright
    Behavior: Turn on light or turn off light

  2. A motorcycle is an object
    Status:Current gear,2wheels,Number of gearsetc.
    Behavior:Brake, accelerate, change gears, etc.

The principles of object-oriented programming:

 The focus of object-oriented programming is to decompose complex programming tasks into objects that contain fields (storing data) and methods (performing operations on fields).

Java class

Before creating an object in Java, a class needs to be defined. A class is the blueprint of an object.

We can consider a class as a blueprint (prototype) of a house. It contains all the details about the floor, doors, windows, etc. Based on these descriptions, we build houses. Houses are objects.

Since many houses can be made with the same description, we can create many objects based on a class.

How to define a class in Java?

This is how we define class methods in Java:

class ClassName {
   // Property
   // method
}

For example,

class Lamp {
  //Instance variable
  private boolean isOn;
  // method
  public void turnOn() {
    isOn = true;
  }
  // method
  public void turnOff() {
  	isOn = false;
  }
}

Here, we create a class named Lamp.

 The class has a variable named isOn and two methods turnOn() and turnOff(). These variables and methods defined in the class are called class members.members.

In the above examples, we used the keywords private and public. These are called access modifiers. For more information, please visitJava Access Modifiers.

Java object

An object is called an instance of a class. For example, if Animal is a class, then Cat, Dog, Horse, etc., can be regarded as objects of the Animal class.

This is how we can create objects with Java:

className object = new className();

 Here, we use the constructor className() to create an object. The constructor has the same name as the class and is similar to a method. For more information about Java constructors, please visitJava Constructors.

Let's see how to create an object of the Lamp class.

// l1 object
Lamp l1 = new Lamp();
// l2 object
Lamp l2 = new Lamp();

 Here, we use the constructor Lamp() to create an object named l1and l2of the object.

An object is used to access class members. Let's create an object of the Lamp class

How to access members?

An object is used to access class members. We can use the . operator to access members (call methods and access instance variables). For example,

class Lamp {
    turnOn() {...}
}
//Create object
Lamp l1 = new Lamp();
//Access method turnOn()
l1.turnOn();

 This statement calls l1the turnOn() method in the Lamp class of the object.

We have mentioned many timesmethodword. You will learn more about it in the next chapterJava Methods.This is what you need to know now:

 When calling the method using the above statement, all statements in the turnOn() method body will be executed. Then, the program control jumps back to l1.turnOn() statement;

Java method workflow diagram

Similarly, you can access instance variables in the following way:

class Lamp {
    boolean isOn;
}
//Create object
Lamp l1 = new Lamp();
// Access method turnOn()
l1.isOn = true;

Example: Java class and object

class Lamp {
    boolean isOn;
    void turnOn() {
        // Initialize a variable with the value true
        isOn = true;
        System.out.println("Light up? ", + isOn);
    }
    void turnOff() {
        //Initialize a variable with the value false
        isOn = false;
        System.out.println("Light up? ", + isOn);
    }
}
class Main {
    public static void main(String[] args) {
  
        //Create object l1and l2
        Lamp l1 = new Lamp();
        Lamp l2 = new Lamp();
  
        //Call the method turnOn() and shutdown()
        l1.turnOn();
        l2.turnOff();
    }
}

Output:

Light up? true
Turn on? false

In the above program

  1. We created a class named Lamp.

  2.  This class has one instance variable isOn and two methods turnOn() and turnOff().

  3. Inside the Main class, we created two objects of the Lamp class, l1and l2.

  4.  Then we use l1The object calls turnOn() using l2The object calls turnOff():  

    l1.turnOn();
    l2.turnOff();
  5.  The function of the method is to: turn off l1The isOn variable of the object is set to true. And the output is printed. Similarly, the turnOff() method will turn off l2The isOn variable of the object is set to false and the output is printed.

Note:Variables defined in a class are called instance variables for a reason. When an object is created, it is called an instance of the class. Each instance contains a copy of the variables defined in the class. Therefore, they are called instance variables. For example, the object l1and l2The isOn variable is different.

In the following tutorials, we will explore more about object-oriented programming in Java.