- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
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:
- Declares a reference variable
obj
- Uses
new
to allocate memory - 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:
- Memory is allocated on the heap.
- A constructor is invoked (if defined).
- 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 toNullPointerException
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.