DEV Community

PONVEL M
PONVEL M

Posted on

Instance Variables and the `new` Keyword in Java: A Complete Guide

When learning Java, two important concepts every beginner should understand are instance variables and the new keyword. These concepts are closely related because instance variables store data for objects, and the new keyword is used to create those objects. In this blog, we will explore both in a simple and practical way.


What is an Instance Variable in Java?

An instance variable is a variable declared inside a class but outside any method, constructor, or block. It belongs to an specific object (instance) of the class.

Each object created from a class gets its own copy of the instance variables. This means different objects can store different values in the same variable.

Example of Instance Variable

class Student {
    int marks;   // instance variable

    void display() {
        System.out.println("Marks: " + marks);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();

        s1.marks = 85;
        s2.marks = 90;

        s1.display();
        s2.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Marks: 85
Marks: 90
Enter fullscreen mode Exit fullscreen mode

In this example, marks is an instance variable. Each object (s1 and s2) has its own value.


Features of Instance Variables

  • Declared inside a class but outside methods
  • Each object has a separate copy
  • Gets default values (0 for int, null for objects, false for boolean)
  • Accessible using object reference
  • Stored in heap memory

What is the new Keyword in Java?

The new keyword in Java is used to create objects. When we use new, Java allocates memory for the object and returns a reference to it.

In simple words, the new keyword helps create an instance of a class.

Syntax of new Keyword

ClassName objectName = new ClassName();
Enter fullscreen mode Exit fullscreen mode

Example

Student s = new Student();
Enter fullscreen mode Exit fullscreen mode

Here:

  • Student is the class name
  • s is the object reference
  • new Student() creates a new object

How new Keyword Works Internally

When we use the new keyword:

  1. Memory is allocated in heap memory
  2. Instance variables are initialized with default values
  3. Constructor of the class is called
  4. A reference to the object is returned

Relationship Between Instance Variables and new

Instance variables come into existence only when an object is created using the new keyword. Without creating an object, instance variables cannot be used.

For example:

Student s = new Student();
s.marks = 75;
Enter fullscreen mode Exit fullscreen mode

Here, the object s is created using new, and then the instance variable marks is accessed.


Conclusion

Instance variables and the new keyword are fundamental concepts in Java programming. Instance variables store object-specific data, while the new keyword is responsible for creating those objects in memory. Understanding these concepts helps programmers build object-oriented applications effectively.

Mastering these basics is an important step toward learning advanced Java concepts like constructors, inheritance, and object-oriented design.

Top comments (0)