---
title: "What is a class and an object in Java?"  
description: "What is a class and an object in Java?"  
author: "Ravi Vishwakarma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/interview/33952/what-is-a-class-and-an-object-in-java  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# What is a class and an object in Java?

#### Class

A class in Java is a blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the objects created from the class will have.

- **Fields (Instance Variables):** Attributes or properties that define the state of the objects.
- **Methods:** Functions that define the behaviors or actions that the objects can perform.
- **Constructors:** Special methods used to initialize new objects.

Example:

```java
public class Car {
    private String color;
    private String model;
    private int year;

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

    public void displayDetails() {
        System.out.println("Car Model: " + model + ", Year: " + year + ", Color: " + color);
    }
}
```

#### Object

An object is an instance of a class. It represents a specific entity with a state defined by the fields and behaviors defined by the methods of the class.

- **State:** The data held in the object's fields.
- **Behavior:** The methods that can be called on the object.

Example:

```java
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Red", "Toyota Camry", 2021);
        myCar.displayDetails(); // Output: Car Model: Toyota Camry, Year: 2021, Color: Red
    }
}
```

Read more

[Understanding Object-Oriented Programming in Java, Core concept and implementation](https://www.mindstick.com/articles/336001/understanding-object-oriented-programming-in-java-core-concepts-and-implementation)

[Explain the 'System.out.println()' and 'System.in'](https://www.mindstick.com/articles/335984/explain-the-system-out-println-and-system-in)

[**What are the differences between final, finally, and finalize in Java?**](https://www.mindstick.com/interview/33951/what-are-the-differences-between-final-finally-and-finalize-in-java)

## Answers

### Answer by Ravi Vishwakarma

#### Class

A class in Java is a blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the objects created from the class will have.

- **Fields (Instance Variables):** Attributes or properties that define the state of the objects.
- **Methods:** Functions that define the behaviors or actions that the objects can perform.
- **Constructors:** Special methods used to initialize new objects.

Example:

```java
public class Car {
    private String color;
    private String model;
    private int year;

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

    public void displayDetails() {
        System.out.println("Car Model: " + model + ", Year: " + year + ", Color: " + color);
    }
}
```

#### Object

An object is an instance of a class. It represents a specific entity with a state defined by the fields and behaviors defined by the methods of the class.

- **State:** The data held in the object's fields.
- **Behavior:** The methods that can be called on the object.

Example:

```java
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Red", "Toyota Camry", 2021);
        myCar.displayDetails(); // Output: Car Model: Toyota Camry, Year: 2021, Color: Red
    }
}
```

Read more

[Understanding Object-Oriented Programming in Java, Core concept and implementation](https://www.mindstick.com/articles/336001/understanding-object-oriented-programming-in-java-core-concepts-and-implementation)

[Explain the 'System.out.println()' and 'System.in'](https://www.mindstick.com/articles/335984/explain-the-system-out-println-and-system-in)

[**What are the differences between final, finally, and finalize in Java?**](https://www.mindstick.com/interview/33951/what-are-the-differences-between-final-finally-and-finalize-in-java)


---

Original Source: https://www.mindstick.com/interview/33952/what-is-a-class-and-an-object-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
