Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Class
Blueprint of Objects



In Java, a class is the fundamental building block of object-oriented programming. Think of it as a blueprint — it defines what an object will look like and how it will behave. Just like an architect’s design isn't a building itself, a class isn't an object — it just describes one.

Why Are Classes Important?

Classes bring structure to your code. Instead of writing repetitive logic, you define it once in a class and reuse it by creating objects. They support abstraction, encapsulation, inheritance, and polymorphism — all pillars of OOP.

How to Declare a Class in Java

Declaring a class is simple. Here's the syntax:

class ClassName {
    // Fields (variables)
    // Methods (functions)
}

Example 1: Defining a Simple Class

Let’s define a class called Car with a few attributes and a method to display its details.

class Car {
    String brand;
    int year;

    void displayDetails() {
        System.out.println("Brand: " + brand);
        System.out.println("Year: " + year);
    }
}

Example 2: Creating Objects from a Class

Once we have a class, we can create objects (instances) from it using the new keyword.

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.brand = "Toyota";
        myCar.year = 2020;
        myCar.displayDetails();
    }
}
Brand: Toyota
Year: 2020

Understanding the Output

Here, we created an object myCar of type Car. We then assigned values to its brand and year fields. Calling displayDetails() printed those values to the console.

Example 3: Adding Constructors

A constructor is a special method used to initialize objects. Let’s modify our Car class to include one.

class Car {
    String brand;
    int year;

    // Constructor
    Car(String b, int y) {
        brand = b;
        year = y;
    }

    void displayDetails() {
        System.out.println("Brand: " + brand);
        System.out.println("Year: " + year);
    }
}
public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Honda", 2018);
        car1.displayDetails();
    }
}
Brand: Honda
Year: 2018

Key Concepts Around Classes

Example 4: Using Multiple Objects

You can create multiple objects from the same class, each with its own set of data.

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Ford", 2021);
        Car car2 = new Car("Tesla", 2022);

        car1.displayDetails();
        System.out.println("---");
        car2.displayDetails();
    }
}
Brand: Ford
Year: 2021
---
Brand: Tesla
Year: 2022

Tips to Remember

QUIZ

Question 1:What does a class represent in Java?

Question 2:In Java, classes can have both variables and methods inside them.

Question 3:Which of the following statements about constructors in Java are true?

Question 4:What is the output of the following code?
Car car1 = new Car("Ford", 2021);
car1.displayDetails();
Assuming the Car class has a constructor and a display method as defined in the tutorial.

Question 5:In Java, you can only create one object from a class.

Question 6:Which of the following are components commonly found inside a Java class?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M