new Keyword in Java (Usage and Examples)28 Apr 2025 | 5 min read The Java `new` keyword creates a class instance by allocating dynamic memory for a new object and returns a reference to that memory. It can also be used to create an array object. When the `new` keyword is employed, it executes the class constructor to initialize the object. So, the new keyword is important in Java, as it creates an instance of the class. Syntax:Usage of new Keyword
Java new Keyword ExamplesCreating an ObjectExampleCompile and RunOutput: Invoking Method Explanation In the above program, we defined a class named Main. Inside the Main class, we declared and defined a method named display(). To invoke the display() method, we first created an object of the Main class in the main() method using the new keyword. We invoked the display() method with that object, which prints 'Invoking Method' on the console. Creating an Object with a Default ConstructorExampleCompile and RunOutput: Invoking Constructor Explanation In the above program, we defined a class named Main. Inside the Main class, there is a default constructor. Within the constructor, there is only a print statement. In the main() method, we have created an object of the Main class that automatically calls the default constructor, which prints 'Invoking Constructor' on the console. Creating an Object with a Parameterized ConstructorExampleCompile and RunOutput: 30 Explanation In the above program, we defined a class named Main. Inside the Main class, there is a parameterized constructor that has two parameters, a and b of type integer, and a method named display(). Inside the main() method, we have created an object of the Main class by using the new keyword and passed the parameters in the constructor. When we invoke the display() method, it prints 30 on the console. Creating an ArrayExampleCompile and RunOutput: Array length: 3 Explanation In the above program, we defined a class named Main in which an instance of an array (of size 3) is created by using the new keyword. The print statement accesses the length property of the arr[] array that returns the size of the array (in this case, 3), and prints it Array length: 3 on the console. Creating CollectionExampleCompile and RunOutput: [Java, C++, Python] Explanation In the above program, we created an object of the ArrayList class using the new keyword and added elements to it. The print statement outputs the elements to the console. Important Points to Remember
Java new Keyword MCQ1. What does the new keyword in Java?
Answer: 3) Explanation: The Java `new` keyword creates a class instance. 2. Which of the following is true about the new Keyword?
Answer: 3) Explanation: The Java `new` keyword creates a class instance by allocating memory for a new object and returns a reference to that memory. 3. What happens if we use the new keyword without assigning it to a reference variable?
Answer: 1) Explanation: When the new keyword is used without assigning the created object to a reference variable, the object is still created, and memory is allocated for it. However, there is no reference pointing to this object; it becomes unreachable after that statement. 4. How many objects will be created in the following?
Answer: 2) Explanation: We know that the new keyword creates an object. Therefore, two objects are created for the first two statements. In the third statement, a string is declared, which creates another object. In the fourth statement, again, a string tpointtech is declared that already exists in the third statement. So, for the fourth statement, it does not create an additional object. Hence, there will be total three objects. 5. Select a valid statement for the creation of an integer array.
Answer: 2) Explanation: int[] arr[] = new int[3] is the correct syntax for declaring an integer array. Next TopicNull-keyword-in-java |
In Java, Banker's algorithm is a deadlock avoidance and resource allocation algorithm. This algorithm tests for security by simulating allocation for a predetermined maximum possible amount of all resources. After that, before deciding whether the allocation should be allowed to continue or not, it creates an...
5 min read
Introduction Abstraction is the process of hiding the details of an entity and focusing on the essential characteristics of the entity. In object-oriented programming, abstraction is an important concept that helps developers to model real-world entities in the code. The Java language completely incorporates abstraction, a key idea...
4 min read
The Stream.skip(long n) method in Java is an important part of the Stream API which was introduced in Java 8. It enables developers to build pipelines of operations on data sets. The skip() method is especially useful for skipping a certain number of elements in...
9 min read
Java provides a robust concurrency framework that allows developers to write efficient and safe multi-threaded applications. Among the many tools and concepts, it offers, atomic classes and the volatile keyword are crucial for ensuring thread safety and visibility of shared variables. In this section, we...
5 min read
In this section, we will learn what is a centered square number and also create Java programs to check if the given number is a centered square number or not. The centered square number program is frequently asked in Java coding interviews and academics. Centered Square Numbers Centered...
6 min read
In Java, HashMap is a non-synchronized (not thread safe) class. It means that if multiple threads want to modify or access the threads at the same time then it can lead unexpected behavior. Because of this HashMap is not suitable for use in multi-threaded applications...
5 min read
In Java programming, the concept of null is both fundamental and ubiquitous. It represents the absence of a value for reference types and serves as a critical tool for developers to handle cases where objects or arrays are not initialized. Understanding null is essential for...
3 min read
The object-oriented programming language Java has a number of capabilities that help developers build adaptable, reusable, and scalable applications. Generics, a potent tool that enables programmers to construct classes, methods, and interfaces that function with many types of data, is one of the fundamental components of...
4 min read
Given the number of terms n, find the sum of the series 0.6, 0.06, 0.006, 0.0006,… up to n terms. Input: n=4 Output: 0.6666 Explanation: Sum of the series up to 4 terms: 0.6+0.06+0.006+0.0006= 0.66660 Input: n=5 Output: 0.66666 Explanation: Sum of the series up to 5 terms: 0.6+0.06+0.006+0.0006+0.00006=0.66666 Approach: Using the Geometric Progression Formula The...
6 min read
Java 8 introduced a number of new features to the programming language, one of the most significant being the lambda expression. Lambda expressions provide a concise way to express a method that can be passed around as an argument to another method, enabling functional programming paradigms...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India