Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java new Keyword
Usage and Examples



new Keyword in Java

In Java, the new keyword is used to dynamically allocate memory for an object or an array. When you use new, you’re telling the Java Virtual Machine (JVM) to create a new instance of a class — that is, a brand new object that lives in the heap memory.

Why is new Important?

Without new, you can't instantiate most classes. It's your go-to keyword when you want to build an object, call a constructor, or create a new array. In essence, it's the bridge between a class (the blueprint) and an object (the real-world thing).

Syntax of the new Keyword

ClassName obj = new ClassName();

This statement does three things:

  1. Declares a reference variable obj
  2. Uses new to allocate memory
  3. Calls the constructor ClassName()

Creating an Object Using new

class Car {
    String color = "Red";
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car(); // using 'new'
        System.out.println(myCar.color);
    }
}
Red

Explanation: We created a new instance of the Car class using the new keyword. The constructor initializes the object, and we access the color field.

Using new to Call a Constructor

class Book {
    String title;

    Book(String t) {
        title = t;
    }
}

public class Main {
    public static void main(String[] args) {
        Book b = new Book("Java Basics");
        System.out.println(b.title);
    }
}
Java Basics

Explanation: The new keyword not only allocates memory, but also invokes the constructor of the Book class, passing the title as an argument.

Using new to Create Arrays

public class Main {
    public static void main(String[] args) {
        int[] numbers = new int[5];
        numbers[0] = 10;
        System.out.println(numbers[0]);
    }
}
10

Explanation: We used new int[5] to create an integer array of size 5. The array is stored in heap memory, and each element is initialized to 0 by default.

Behind the Scenes: What Happens When You Use new?

Here's a simplified view of what happens when new is used:

  1. Memory is allocated on the heap.
  2. A constructor is invoked (if defined).
  3. A reference to the object is returned to the calling variable.

This is key to Java’s memory model: objects live on the heap, and references to them are stored in variables.

Can You Use a Class Without new?

Yes, but only in special cases. For example, when working with String literals:

String s1 = "Hello"; // No 'new' used
String s2 = new String("Hello"); // With 'new'

The first line uses the string pool, which is more memory-efficient. The second creates a new object on the heap, even if the content is the same.

Using new with Anonymous Classes

You can also use new to create anonymous classes:

Runnable r = new Runnable() {
    public void run() {
        System.out.println("Running!");
    }
};
r.run();
Running!

Explanation: Here, new is used to instantiate an unnamed class that implements Runnable.

Common Pitfalls with new



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