Java Class

Summary: in this tutorial, you will learn about Java class and how to define a custom class in Java.

Introduction to the Java class

Objects are the fundamental building block of object-oriented programming (OOP). Objects allow you to model real-world problems.

Objects have data (fields) and behaviors (methods):

  • Fields represent the data of the object.
  • Behaviors represent the actions that objects can do to manipulate their data.

Before creating objects, you need to define a class. A class is a blueprint or template for creating objects.

Defining a Java class

To define a class, you use the class keyword followed by the class name. For example, the following defines the Person class:

class Person {
}Code language: Java (java)

By convention, the class name follows the Pascal case in which you capitalize the first letter of each word in the class name, for example, Person, SalesPerson, etc.

Also, you need to place each class in a separate Java source code file and the source code file should have the same name as the class name.

For example, if the class name is Person, the filename should be Person.java.

Creating objects from the class

To create an object from a class, you use the new keyword followed by the class’s constructor:

Person person = new Person();Code language: Java (java)

The Person() is the constructor of the Person class. Note that you’ll learn more about the constructor later.

Since Java can infer the type of the person variable, you can use the var keyword to make the code more concise:

var person = new Person();Code language: Java (java)

The person variable is an object of the Person class. It is also known as an instance of the Person class.

Defining fields

Objects have fields. For example, person objects may have a name and age.

Fields can have various data types including primitive data types and custom types. For example, the name field has the type of String and the age field has the type of int.

The following shows how to define the name and age properties in the Person class. It is like declaring variables inside a class:

class Person {
    String name;
    byte age;
}Code language: Java (java)

Since we define the name and age properties in the Person class, they will be available in all the objects of the Person class.

It means that you can assign values to or read values from the name and age fields of the objects of the Person class.

For example, the following creates a new Person object, sets the values for the name and age properties with the values "John" and 22, and display the name and age information.

var person = new Person();

person.name = "John";
person.age = 22;

System.out.printf("%s %d", person.name, person.age);Code language: Java (java)

Output:

John 22Code language: Java (java)

Defining methods

Methods define the behaviors of objects. They are the actions that objects can perform.

The following example shows how to define a method called sayHi() in the Person class:

class Person {
    String name;
    byte age;

    void sayHi() {
        System.out.printf("Hi. It's %s.", name);
    }
}
Code language: Java (java)

The sayHi() method accepts no argument and doesn’t return any value. It displays a Hi message and the name of the person.

To call a method of an object, you use the object name, followed by the dot operator, and the method name:

objectName.methodName();Code language: CSS (css)

For example, the following shows how to call the sayHi() method of the person object:

var person = new Person();

person.name = "John";
person.age = 22;

person.sayHi();Code language: Java (java)

Output:

Hi. It's John.Code language: Java (java)

Summary

  • Objects have fields (data) and methods (behaviors).
  • Fields represent the data of the object and the method defines actions that the object can perform.
  • Class is a blueprint for creating objects.
  • Use the class keyword to define a class.
  • Use the new keyword to define an object of a class.