All Exams Test series for 1 year @ ₹349 only
Question

Which keyword is used to create an instance of a class in most programming languages?

The correct answer is

new

Understanding How to Create an Instance of a Class

In object-oriented programming (OOP), a class is a blueprint for creating objects. An object is an instance of a class. When you want to use the properties (variables) and behaviors (methods) defined in a class, you need to create an instance of that class. This process is often called instantiation.

To create an instance of a class, most programming languages use a specific keyword. Let's look at the options provided and identify the correct keyword used for this purpose.

Analyzing the Options for Creating Class Instances

  • alloc: The term "alloc" is often associated with memory allocation (short for "allocate"), particularly in languages like Objective-C, where you might first allocate memory and then initialize the object. However, it is not the standard keyword used in most common languages solely for creating an instance.
  • new: The keyword "new" is widely used across many popular programming languages like Java, C++, C#, JavaScript, and others, specifically to create an instance of a class. When you use new followed by the class name and constructor arguments, the system allocates memory for the new object and calls the class's constructor to initialize it. This is the primary way to perform object creation.
  • create: While the word "create" accurately describes the action of making an instance, it is generally not the specific keyword used in the syntax of most programming languages for this operation.
  • instance: The word "instance" refers to the object itself (an instance of the class), but it is not a keyword used to perform the action of creating the instance in the code syntax.

The Role of the 'new' Keyword in Object Creation

The keyword "new" is fundamental to object creation in many programming languages. It signals the runtime environment to perform the necessary steps to bring an object into existence based on its class definition. This typically involves:

  1. Allocating memory on the heap for the new instance variables of the object.
  2. Initializing the object's state by calling the appropriate constructor of the class.
  3. Returning a reference (or pointer) to the newly created object.

This process is crucial for working with object-oriented programming principles, allowing you to have multiple distinct objects (instances) of the same class, each with its own set of data.

Here are examples of how the keyword new is used to create instance of class in different languages:

Language Syntax to Create Instance
Java MyClass obj = new MyClass();
C++ MyClass* obj = new MyClass();
C# MyClass obj = new MyClass();
JavaScript let obj = new MyClass();
PHP $obj = new MyClass();

These examples demonstrate the consistent use of the keyword new across various popular languages for class instantiation and memory allocation for the new object.

Conclusion

Based on the analysis of common programming languages and their syntax for object creation, the keyword used to create an instance of a class is "new". This keyword is a cornerstone of object-oriented programming when working with classes and objects.

Was this answer helpful?

Important Questions from Java

  1. When an applet begins, which of the following sequence methods is CORRECT?

    1) start()

    2) paint()

    3) init()

  2. What will be the output of the following Java program?

    class test

    {public static void main (String args [ ])

    {

    System.out.println(20 + 30 + "Java");

    System.out.println("Java" + 20 + 30);

    }

    }

  3. In Java, the 'PreparedStatement' interface is a sub interface which is used to execute __________ query.

  4. Which of the following is NOT a java primitive type?

  5. Which of the following is/are the rules to declare variables in Java?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App