Java 'new' Keyword Tutorial

new

The new keyword in Java is used to create new objects. It instantiates a class by allocating memory for a new object and returning a reference to that memory. The new keyword also invokes the object constructor.

Usage

The new keyword is typically used in the following way:

ClassName object = new ClassName();
  • ClassName: Name of the class to be instantiated.
  • object: Name of the reference variable.
  • new: Keyword to instantiate the class.
  • ClassName(): The constructor of the class.

Example

public class Student {
  void study() { System.out.println("studying..."); }
  
  public static void main(String args[]) {
    Student s = new Student(); // creating object
    s.study();
  }
}

Memory Allocation

When you create an object using the new keyword, memory is allocated in the Heap area.

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.