The new Keyword in Java
The new keyword in Java creates objects. Every time you write
new Something(), Java builds a fresh object in memory and hands you a way to reach it. It looks
like a single step, but four things happen behind the scenes, and knowing them explains many common Java bugs.
What does new do in Java?
A class is only a blueprint. new is the builder that
turns the blueprint into a real object you can use:
Book book = new Book("Dune", 412); Book book— declares a variable that can hold a reference to a Book.new Book("Dune", 412)— creates the object and runs its constructor with those arguments.=— stores the reference inbook.
What happens step by step when you call new?
The 4 steps behind new Book("Dune", 412)
| Step | What Java does | State of the object |
|---|---|---|
| 1. Allocate | Reserves memory for the object on the heap | Empty space |
| 2. Defaults | Sets every field to its default value | title = null, pages = 0 |
| 3. Construct | Runs the constructor (after the parent constructors) | title = "Dune", pages = 412 |
| 4. Return | Hands back a reference to the object | Stored in book |
The variable never holds the book itself, only its "address" on the heap. That's why two variables can point to the same object.
What are the default values?
Before the constructor runs, every field starts at a safe default:
| Field type | Default value |
|---|---|
int, long, short, byte | 0 |
double, float | 0.0 |
boolean | false |
char | '\u0000' (the null character) |
Any object type (String, List, …) | null |
Local variables inside methods don't get defaults. Java makes you assign them before use.
Let's see it in action
class Book {
String title;
int pages;
boolean borrowed;
Book() { } // leaves the defaults alone
Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
}
Book blank = new Book();
Book dune = new Book("Dune", 412);
System.out.println(blank.title + ", " + blank.pages + ", " + blank.borrowed);
System.out.println(dune.title + ", " + dune.pages + ", " + dune.borrowed); null, 0, false Dune, 412, false
Using new to create arrays
Arrays are objects too, so they're also created with new:
int[] scores = new int[3]; // [0, 0, 0] — defaults again
String[] names = new String[2]; // [null, null]
int[] primes = new int[] {2, 3, 5, 7}; // create and fill
int[] evens = {2, 4, 6}; // shorthand, only when declaring
System.out.println(java.util.Arrays.toString(scores)); [0, 0, 0]
Watch out: new Book[3] creates an array of three empty slots (null), not three books. You still need new Book(...) for each one.
String literal vs new String(): a classic interview question
String a = "hello";
String b = "hello";
String c = new String("hello");
System.out.println(a == b); // same pooled object?
System.out.println(a == c); // c is a separate copy
System.out.println(a.equals(c)); // same characters? true false true
String literals are stored once in Java's string pool and reused, so a and
b share an object. new String(...) always makes a new copy. The lesson: compare
strings with equals(), never ==, and you'll rarely need new String(...).
Do you always need new?
No. Modern Java often hides new behind factory methods, which are static methods that create objects for you:
| Instead of… | You'll often see… | Why |
|---|---|---|
new ArrayList<>(...) for fixed data | List.of("a", "b") | Shorter, and the list can't be modified |
new Integer(5) (deprecated for removal) | Integer.valueOf(5) or just 5 | Reuses cached objects |
| Calculating today's date yourself | LocalDate.now() | Descriptive name, handles the details |
What about abstract classes and interfaces?
You can't write new for an abstract class or an
interface, because they're unfinished. You can create an
anonymous class that finishes them on the spot, though for a single-method interface a lambda is usually neater:
Runnable task = new Runnable() { // anonymous class
@Override
public void run() {
System.out.println("Running!");
}
};
Runnable sameTask = () -> System.out.println("Running!"); // lambda: same thing, shorter What happens to objects you no longer use?
Java cleans up for you. When no variable refers to an object anymore, it becomes eligible for
garbage collection and its memory is reclaimed automatically. There's no delete
in Java. Calling new inside a tight loop is fine for small objects; modern garbage collectors are very fast.
Quick recap
newallocates heap memory, sets defaults, runs the constructor, and returns a reference.- Fields start at
0,false, ornulluntil the constructor sets them. - Arrays are created with
newtoo, and object arrays start full ofnull. - Compare strings with
equals(); literals are pooled,new Stringisn't. - Factory methods like
List.of()often replacenewin modern code.
What's next?
Objects can fail too. Learn how Java signals errors in throw vs throws, or revisit classes and objects.
Frequently Asked Questions
What does the new keyword do in Java?
new creates a brand-new object. It reserves memory on the heap, sets every field to a default value, runs the constructor, and gives you back a reference to the finished object that you can store in a variable.
Can you create an object without new in Java?
Yes, in a few ways. String literals like "hi" and autoboxing like Integer x = 5 create objects for you, and factory methods such as List.of() or LocalDate.now() call new internally. Reflection, cloning, and deserialization can also create objects.
What is the difference between String s = "hi" and new String("hi")?
A literal like "hi" is reused from Java's string pool, so two identical literals share one object. new String("hi") always creates a separate copy. That's why == can give different results. Always compare strings with equals(), and avoid new String(...) in normal code.
Do I need to delete objects created with new?
No. Java has automatic garbage collection. When no variable refers to an object anymore, the garbage collector frees its memory for you. There's no delete keyword in Java.
Can you use new with an abstract class or interface?
Not directly. new Shape() fails if Shape is abstract or an interface. You can only use new with an anonymous class that fills in the missing methods on the spot, or, for a functional interface, a lambda instead.