> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/oop-basics/what-is-an-object.md).

# What is an Object?

## About

In Java, an **object** is an instance of a class that contains **state (fields/variables)** and **behavior (methods)**. Objects are created based on the blueprint defined by a class.

## **Characteristics of an Object**

1. **State (Attributes)**&#x20;
   * Represented by **instance variables** (fields).
   * Stores the data or properties of the object.
2. **Behavior (Methods)**&#x20;
   * Defined by **functions inside the class**.
   * Determines what actions an object can perform.
3. **Identity (Reference)**&#x20;
   * Each object has a unique memory address in the heap.

## **Example of an Object**

```java
// Class Definition
class Car {
    String brand;  // Attribute
    int speed;

    // Constructor
    Car(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }

    // Method (Behavior)
    void accelerate() {
        speed += 10;
        System.out.println(brand + " is now going at " + speed + " km/h.");
    }
}

// Main Class
public class Main {
    public static void main(String[] args) {
        // Creating Objects
        Car car1 = new Car("Toyota", 50);
        Car car2 = new Car("Honda", 40);

        // Accessing Object Methods
        car1.accelerate();  // Toyota is now going at 60 km/h.
        car2.accelerate();  // Honda is now going at 50 km/h.
    }
}
```

## **How Objects Work in Memory**

1. Objects are created in the **heap memory**.
2. The **reference variable** (like `car1` and `car2`) points to the object in memory.
3. Multiple reference variables can point to the same object.

```java
Car car3 = car1;  // Now both car1 and car3 point to the same object
```

## When is the object created with new keyword

In Java, the object is created with the `new` keyword during **runtime**.

The `new` keyword plays a crucial role in the object creation process:

1. **Memory Allocation:** It triggers the allocation of memory for the new object on the heap. The heap is a special area of memory dedicated to storing objects in Java.
2. **Constructor Call:** It invokes the constructor of the class specified after `new`. The constructor is responsible for initializing the object's state by assigning values to its fields.

Therefore, the `new` keyword initiates object creation and memory allocation at runtime, not during compilation. This allows for dynamic object creation based on your program's needs.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/oop-basics/what-is-an-object.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
