Java Class - Blueprint of Objects

What is a Class?

A class in Java is like a plan or template for creating things (objects). It tells what data the object will have and what it can do. But the class itself is not the actual thing — it's just a description of it. Just like an architect’s design isn't a building itself, a class isn't an object — it just describes one.

Importance of Classes

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 and we shall learn each of them in detail in our next classes.

How to Declare a Class in Java

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

class ClassName {
    // Fields (variables)
    // Methods (functions)
}
  • class is the keyword used to declare a class.
  • ClassName is the name you give to your class. By convention, class names start with a capital letter.
  • The class body is enclosed in curly braces { }.
  • Inside the class, you can define:
    • Fields: variables that hold data for each object.
    • Methods: functions that define what the object can do.

Defining a 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);
    }
}

Creating Objects from a Class

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

class Car {
    String brand;
    int year;

    void displayDetails() {
        System.out.println("Brand: " + brand);
        System.out.println("Year: " + year);
    }
}
    
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

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

  • Fields: Variables declared inside a class that define the state of an object.
  • Methods: Functions defined in a class that describe its behavior.
  • Constructors: Special methods to initialize objects. If not defined, Java provides a default one.
  • Access Modifiers: Such as public, private, and protected to control visibility.

Example 4: Using Multiple Objects

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

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("Ford", 2021);
        Car car2 = new Car("Tesla", 2022);

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

Remember

  • Class name should start with an uppercase letter by convention.
  • You can create as many objects from a class as you need.
  • Fields store data; methods manipulate or display it.
  • Constructor names must match the class name.

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?


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...