Java Class

Introduction to Java Class

A Java class is the basic building block of object-oriented programming in Java. Java is an object-oriented programming language that is widely used in developing web and mobile applications. It is a blueprint or template that defines the properties and behavior of objects that are created from it. In this tutorial, we will explore the concept of Java class, its components, and how to create and use it in code.

Components of a Java Class

A Java class consists of the following components:

  1. Class declaration: The class declaration defines the name of the class and the access modifier, which determines the visibility of the class to other classes.
  2. Fields or instance variables: Fields are the variables that hold the state of an object. They define the properties or attributes of an object that are unique to it.
  3. Constructors: Constructors are special methods that are used to initialize the state of an object when it is created. They have the same name as the class and no return type.
  4. Methods: Methods define the behavior or actions of an object. They are functions that can manipulate the state of an object or return a value.
  5. Inner classes: Inner classes are classes that are defined inside another class. They can access the private members of the enclosing class.

Creating a Java Class

To create a Java class, follow these steps:

  1. Open your Java development environment (IDE) and create a new Java project.
  2. Create a new Java class file by selecting File > New > Class.
  3. Enter the name of the class and select the access modifier.
  4. Define the fields, constructors, and methods of the class.

Here is an example of a Java class that represents a car:

public class Car {
   private String make;
   private String model;
   private int year;

   public Car(String make, String model, int year) {
      this.make = make;
      this.model = model;
      this.year = year;

   public String getMake() {
      return make;

   public void setMake(String make) {
      this.make = make;

   public String getModel() {
      return model;

   public void setModel(String model) {
      this.model = model;

   public int getYear() {
      return year;

   public void setYear(int year) {
      this.year = year;

In the example above, the Car class has three fields (make, model, and year), a constructor that initializes the state of the object, and getter and setter methods for each field.

Using a Java Class

To use a Java class, follow these steps:

  1. Instantiate the class by creating an object from it.
  2. Access the fields and methods of the object using dot notation.

Here is an example of how to use the Car class:

public class CarApp {
   public static void main(String[] args) {
      Car myCar = new Car("Toyota", "Camry", 2020);

      System.out.println("Make: " + myCar.getMake());
      System.out.println("Model: " + myCar.getModel());
      System.out.println("Year: " + myCar.getYear());

      System.out.println("Updated Year: " + myCar.getYear());


Java Class

In the example above, we created an object of the Car class and accessed its fields and methods using dot notation. We also updated the value of the year field using the setYear() method.

Table to summarize some characteristics of a Java class

NameA unique name that identifies the class
VariablesData members that hold values and state for the class
MethodsFunctions that define the behavior and actions of the class
ConstructorsSpecial methods that create instances of the class
Access modifiersKeywords that specify the visibility and accessibility of class members
InheritanceThe ability of a class to inherit properties and methods from a parent class
InterfacesContracts that define a set of methods that a class must implement
PackagesA way to group related classes together for organization and access control
AnnotationsMetadata that provides additional information about a class or its members
Static membersMembers that are associated with the class itself rather than instances of the class
Inner classesClasses that are defined within another class

Note that this table is not exhaustive and there may be additional characteristics that apply to specific use cases.


In this tutorial, we learned about Java classes, their components, and how to create and use them in code. Java classes are the basic building blocks of object-oriented programming and allow developers to

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author