⬅ Previous Topic
Java native KeywordNext Topic ⮕
Java null Keywordnew
Keyword⬅ Previous Topic
Java native KeywordNext Topic ⮕
Java null Keywordnew
Keyword in JavaIn 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.
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).
new
KeywordClassName obj = new ClassName();
This statement does three things:
obj
new
to allocate memoryClassName()
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.
new
to Call a Constructorclass 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.
new
to Create Arrayspublic 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.
new
?Here's a simplified view of what happens when new
is used:
This is key to Java’s memory model: objects live on the heap, and references to them are stored in variables.
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.
new
with Anonymous ClassesYou 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
.
new
new
when creating objects: leads to NullPointerException
when methods or fields are accessed.new
allocates fresh memory, so repeated use may lead to excess heap usage if not managed well.⬅ Previous Topic
Java native KeywordNext Topic ⮕
Java null KeywordYou 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.