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 instanceIn 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…

  1. A.

    Defines a new method

  2. B.

    Imports a package

  3. C.

    Creates a new object instance

  4. D.

    Declares a new variable

  5. 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.

Explore the full course: Rssb Basic Computer Instructor

Loading lesson…