What is the purpose of the ‘new’ keyword in Java?
2024
What is the purpose of the ‘new’ keyword in Java?
Answer: C. Creates a new object instance — In Java, the new keyword is used to create an object (instance) of a class. When new is used: Memory is allocated for the object. The constructor of the class…
- A.
Defines a new method
- B.
Imports a package
- C.
Creates a new object instance
- D.
Declares a new variable
- E.
Question not attempted
Attempted by 531 students.
Show answer & explanation
Correct answer: C
In Java, the new keyword is used to create an object (instance) of a class.
When new is used:
Memory is allocated for the object.
The constructor of the class is called.
A new object instance is created.
Example
class Student {
int id;
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
}
}Here, new Student() creates a new object of the Student class.
Loading lesson…