BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is new Keyword in Java

By Chaitanya Singh | Filed Under: java

The new keyword is used to create an object of a class.

  • It allocates a memory to the object during runtime.
  • It invokes the specified constructor of the class to create the object.

Java new Keyword Example

Here, we have a class JavaExample, which contains a variable and a method. In order to access the variable and method, we need to create an object of this class. In this example, we have created an object obj of this class using new keyword.

public class JavaExample {
  int num = 100; //variable
  public void demo(){
    System.out.println("hello");
  }

  public static void main(String[] args) {
    JavaExample obj = new JavaExample();
    System.out.println(obj.num);
    obj.demo();
  }
}

Output:

100
hello

In the above example, we have used the following statement to create the object.

JavaExample obj = new JavaExample();

This object creation can be divided into three parts:

Declaration: Class name followed by object name. This creates a reference of the class.

JavaExample obj

Instantiation: new keyword followed by the constructor of the class. This creates an object of the class, whose constructor is being called.

new JavaExample();

Initialization: Initializing the reference of class with the created object.

JavaExample obj = new JavaExample();

Example 2: new keyword with array

Here, we are using the new keyword to create an array object.

public class JavaExample {
  public static void main(String[] args) {
    //int array arr of size 3
    int arr[] = new int[3];
    arr[0] = 101;
    arr[1] = 102;
    arr[2] = 103;
    for(int i: arr){
      System.out.println(i);
    }
  }
}

Output:

101
102
103

Example 3: new keyword with strings

We can also create an object of String class using new keyword. There is a difference between simple assignment and assignment using new, the difference is covered in Java String Tutorial.

public class JavaExample {
  public static void main(String[] args) {
    String s1 = new String("Hello");
    String s2 = new String("World!");
    System.out.println(s1+" "+s2);
  }
}

Output:

Hello World!

Example 4: Creating an object of a collection class

We can also use new keyword to create object of various collection classes. In the following example, we are creating an object of ArrayList class.

import java.util.ArrayList;
public class JavaExample {
  public static void main(String[] args) {
    ArrayList<Integer> arrList = new ArrayList<>();
    arrList.add(101);
    arrList.add(201);
    arrList.add(301);
    arrList.add(401);
    for(int i: arrList){
      System.out.println(i);
    }
  }
}

Output:

new keyword to create object example

Recommended Posts

  • Java Constructor Tutorial
  • Java class Keyword
  • Java OOPs Concepts Tutorial
❮ Java Keywords

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap