The new keyword in Java is used to create new objects. When you use new, it allocates memory for a new object and returns a reference to that memory.
Table of Contents
- Introduction
newKeyword Syntax- Understanding
new - Examples
- Creating a New Object
- Using
newwith Arrays - Using
newwith Classes
- Real-World Use Case
- Conclusion
Introduction
The new keyword is essential in Java for creating new instances of classes (objects) and arrays. It dynamically allocates memory for the new object and returns a reference to that object.
new Keyword Syntax
The syntax for using the new keyword is as follows:
ClassName objectName = new ClassName();
Example:
Car myCar = new Car();
Understanding new
When you create an object using new, it performs the following steps:
- Allocates memory for the new object.
- Calls the constructor of the class to initialize the object.
- Returns a reference to the newly created object.
Examples
Creating a New Object
Let’s create a new object of the Car class.
Example
class Car {
String color;
String model;
void drive() {
System.out.println("The car is driving.");
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car(); // Creating a new Car object
myCar.color = "Red";
myCar.model = "Toyota";
System.out.println("Car model: " + myCar.model);
myCar.drive();
}
}
Output:
Car model: Toyota
The car is driving.
Using new with Arrays
You can also use new to create arrays.
Example
public class Main {
public static void main(String[] args) {
int[] numbers = new int[5]; // Creating a new array of integers
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Using new with Classes
You can use new to create instances of any class.
Example
class Person {
String name;
int age;
// Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Person person = new Person("Alice", 30); // Creating a new Person object
person.display();
}
}
Output:
Name: Alice, Age: 30
Real-World Use Case
Creating a Bank Account Class
In real-world applications, you might use the new keyword to create instances of classes that represent real-world entities, such as bank accounts.
Example
class BankAccount {
String accountNumber;
double balance;
// Constructor
BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
System.out.println("Deposited: " + amount);
}
void displayBalance() {
System.out.println("Account Number: " + accountNumber + ", Balance: " + balance);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("123456789", 500.0); // Creating a new BankAccount object
account.deposit(150.0);
account.displayBalance();
}
}
Output:
Deposited: 150.0
Account Number: 123456789, Balance: 650.0
Conclusion
The new keyword in Java is used to create new objects and arrays dynamically. It is a fundamental part of object-oriented programming in Java, enabling the creation and initialization of instances of classes. Understanding how to use the new keyword is essential for working with objects and arrays in Java.