What is an instance in Java?

An instance of a Class means creating an object for the Class. An instance is created with a new keyword. The new operator invokes the object constructor also.

For example, if the Class name is Demo, the syntax for creating an instance is

Demo obj = new Demo();

An object has a state and behavior. When an object is created memory is allocated for the object in the heap a reference to the memory is returned.

The word instance the relationship of an object to its Class. An object is an instance of a class and an instance means representative of the class.

An instance will have both class definition and object definition. A single object can have more than one instance.

The example program below illustrates creating an instance

public class Demo {
  public Demo(String name) {
    // parameter is passed to this constructor while creating an instance
    System.out.println("The object passed is :" + name);
  }
  public static void main(String[] args) {
    // creating an instance of class Demo
    Demo d = new Demo("Car");
  }
}

The output is

The object passed is :Car

Instance variable

An instance variable is a non-static variable that is defined inside a class but outside any method, constructor, or block. They are called instance variables because their values are instance-specific and they are not shared among instances.

Instance variables are created when an object is created with a new keyword and destroyed when the object is destroyed.

An instance variable belongs to the Class. Access modifiers are can be mentioned for the instance variables. Each instantiated object has a separate copy or instance of the variable.

The instance variable can get a default value meaning the instance variable can be used without initializing it. The default value for numbers is 0, for boolean the default value is false, and for object references it is null.

The values for the instance variable can be assigned during the declaration or within the constructor. The instance variables can be accessed directly by calling the variable name inside the class.

Instance variable can be declared at a Class level before or after use. The instance variables are visible to all methods, constructors, and blocks in the Class.

It is recommended to make the instance variables private. But the visibility for subclasses can be given to the instance variables with the use of access modifiers.

In Java when space is allocated for an object in heap, a slot for each instance variable is created. Instance variables can be used only through object reference.

The instance variables hold the values that must be referenced by more than one method, constructor, or block of an object's state that must be present throughout the Class.

Since every object will have its own copy of instance variables, any changes made to the instance variables through one object will not reflect in another object.

Let us consider a program to illustrate the use of instance variable with different access specifiers.

import java.util.Scanner;
public class Student {
  public String name; // this instance variable is visible to any child class
  private int marks; // this instance variable is visible only to Student class
  public Student(String SName) {
    name = SName;
  }
  public void setMarks(int SMarks) {
    marks = SMarks;
  }
  // This method prints the student details.
  public void print() {
    System.out.println("Name: " + name);
    System.out.println("Marks:" + marks);
  }
  public static void main(String args[]) {
    Student St1 = new Student("Alice");
    Student St2 = new Student("Bob");
    Student St3 = new Student("John");
    St1.setMarks(98);
    St2.setMarks(89);
    St3.setMarks(90);
    St1.print();
    St2.print();
    St3.print();
  }
}

The output is

Name: Alice
Marks:98
Name: Bob
Marks:89
Name: John
Marks:90

Instance method

An instance method in Java is a method of the Class. It is a non-static method that is declared inside the Class.

To call an instance method, an object of its class is required. To invoke an instance method, an object has to be created.

Instance methods belong to the object and not to the Class. The instance methods are stored in a single memory location and they know which object they belong to because this pointer is passed while calling them.

The instance method does not require any keyword for declaration. The instance methods have multiple copies depending on the number of instances created for that particular Class.

The static variables and static methods can be accessed with the help of instance methods but the instance variable and instance methods cannot be accessed using static methods.

The example program below illustrates how to use instance methods.

import java.io.*;
class Student {
  String name = "";
  // Instance method to be called within the same class or from another class
  public void print(String name) {
    this.name = name;
  }
}
public class Teacher {
  public static void main(String[] args) {
    // create an instance of the class
    Student st = new Student();
    // calling an instance method in the class 'Student'
    st.print("Java Programming");
    System.out.println(st.name);
  }
}

The output is

Java Programming
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions