Java Reference Library →

new Keyword Usage and Examples

new Keyword Usage and Examples

Learn how the Java 'new' keyword is used to dynamically create objects, arrays, and call constructors. Explore its syntax, real-world examples, and memory allocation behavior.

This reference explains new Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

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

  • Forgetting to use new when creating objects: leads to NullPointerException when methods or fields are accessed.
  • Misunderstanding memory: every use of new allocates fresh memory, so repeated use may lead to excess heap usage if not managed well.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →