Java Instance Members - Variables, Methods and Blocks

⏱️ 15 min read • Beginner Level • Lesson 46

Lesson 46 of 124 of Java Tutorial
You have completed this lesson

Completed on . You can revise this lesson or continue to the next topic.

Instance members in Java are non-static members that belong to an object. Every object gets its own copy of instance variables, and instance methods work with object-level data.

This lesson deeply explains instance variables, instance methods, instance initializer blocks, object memory behavior, tricky cases, common mistakes, and interview questions. Before this lesson, review Java Class Members and Java Class and Objects.


What are Instance Members in Java?

Instance members are members of a class that belong to objects. They are called instance members because they are related to an instance of a class. In Java, an object is also called an instance.

Simple Meaning: If a member is not declared with static and it belongs to an object, it is an instance member.

Real-World Analogy

Think about a Student class. Every student has a different name, roll number, and marks.

  • Student name → different for each object.
  • Roll number → different for each object.
  • Marks → different for each object.

These are object-specific values, so they should be instance variables.

Types of Instance Members

Instance MemberMeaningExample
Instance VariableObject-level data.String name;
Instance MethodObject-level behavior.void show()
Instance Initializer BlockRuns every time an object is created.{ ... }

Instance Variables

An instance variable is declared inside a class but outside any method, constructor, or block. It is not declared with the static keyword. Each object gets a separate copy.

InstanceVariableExample.java
Copy Try Download
class Student {
    String name; // instance variable

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

        s1.name = "Ayan";
        s2.name = "Sarah";

        System.out.println(s1.name);
        System.out.println(s2.name);
    }
}

Output:

Ayan
Sarah

Code Explanation

  1. s1 and s2 are two different objects.
  2. Each object gets its own copy of the name instance variable.
  3. Changing s1.name does not affect s2.name.

Instance Methods

An instance method belongs to an object. It can directly access instance variables and instance methods of the same class. To call an instance method from main(), create an object first.

InstanceMethodExample.java
Copy Try Download
class Student {
    String name = "Ayan";

    void showName() {
        System.out.println(name);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.showName();
    }
}

Output:

Ayan

Code Explanation

  1. showName() is an instance method.
  2. It accesses the instance variable name directly.
  3. The method is called using object reference s.

Instance Initializer Block

An instance initializer block executes every time an object is created. It executes before the constructor body. It is useful when multiple constructors need common initialization code.

InstanceBlockExample.java
Copy Try Download
class InstanceBlockExample {
    int number;

    {
        number = 100;
        System.out.println("Instance block executed");
    }

    InstanceBlockExample() {
        System.out.println("Constructor executed");
    }

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

Output:

Instance block executed
Constructor executed
100

Code Explanation

  1. The instance block initializes number.
  2. The instance block runs before the constructor body.
  3. The constructor runs after the instance block.

Object Memory Diagram

Each object has a separate copy of instance variables.

Student s1
name = "Ayan"
roll = 101
Student s2
name = "Sarah"
roll = 102

Tricky Cases: If This, Then What Happens?

1. If you call an instance method directly from static main?

InstanceMethodError.java
Copy Try Download
class Test {
    void show() {
        System.out.println("Hello");
    }

    public static void main(String[] args) {
        show(); // Error
    }
}

Output:

Compilation Error

Code Explanation

  1. main() is static.
  2. A static method cannot directly call an instance method.
  3. Create an object first, then call obj.show().

2. If object reference is null and you access instance member?

NullReferenceExample.java
Copy Try Download
class Test {
    int number = 10;

    public static void main(String[] args) {
        Test obj = null;
        System.out.println(obj.number);
    }
}

Output:

Runtime Error: NullPointerException

Code Explanation

  1. The reference variable obj does not point to any object.
  2. Instance variables need an actual object.
  3. Accessing an instance member using null causes NullPointerException.

Instance Members vs Static Members

FeatureInstance MemberStatic Member
Belongs toObjectClass
AccessObject referenceClass name
Memory CopySeparate copy per objectSingle shared copy
KeywordNo staticUses static

Common Mistakes

  • Calling instance methods directly from static methods without creating an object.
  • Thinking instance variables are shared by all objects.
  • Confusing instance variables with local variables.
  • Accessing instance members using a null reference.
  • Using object-level data as static data by mistake.
Summary:
  • Instance members belong to objects.
  • Instance variables have separate copies for each object.
  • Instance methods are called using object references.
  • Instance initializer blocks run before constructor body.
  • Static methods cannot directly access instance members without an object.

Interview Questions ⭐

Instance members are non-static members that belong to objects. They include instance variables, instance methods, and instance initializer blocks.

An instance variable is a non-static variable declared inside a class but outside methods, constructors, and blocks. Each object gets its own copy of instance variables.

No. Each object gets a separate copy of instance variables. If one object changes its instance variable, it does not affect the instance variable of another object.

Yes, instance methods can access both instance members and static members directly.

No. A static method cannot directly access instance variables because instance variables belong to objects. A static method needs an object reference to access instance variables.

An instance initializer block is executed every time an object is created, before the constructor body executes.

An instance variable belongs to an object and is declared inside a class but outside methods and blocks. A local variable belongs to a method, constructor, or block scope and can be used only inside that area.

Yes. Instance variables get default values automatically. For example, int gets 0, boolean gets false, and reference types such as String get null.

An instance method is a non-static method that belongs to an object. It can directly access instance variables and other instance methods of the same class.

Because the main method is static, we usually create an object first and then call the instance method using the object reference.

An instance initializer block is a block of code written inside a class without the static keyword. It runs every time an object is created and executes before the constructor body.

Instance variables are used to store object-specific data. For example, each Student object may have a different name, roll number, and marks.

Instance methods are used to define object-specific behavior. They can work with the instance variables of the current object.

Yes. In good object-oriented design, instance variables are often declared private and accessed through getter and setter methods when needed.

Yes, instance variables can be public, but it is generally not recommended because public instance variables reduce encapsulation and expose object data directly.

Yes, instance variables can be final. A final instance variable must be initialized either at declaration, in an instance initializer block, or inside every constructor.

No. If a variable is declared with static, it becomes a static variable, not an instance variable. Instance variables are non-static.

Yes, instance methods can be final. A final instance method cannot be overridden in a subclass.

No. If a method is declared with static, it becomes a static method, not an instance method. Instance methods are non-static methods.

Yes, instance methods can access local variables declared inside the method, but those local variables can be used only within their scope.

If an object reference is null and we try to access an instance variable or instance method using that reference, Java throws NullPointerException at runtime.

An instance member belongs to an object and each object can have its own copy of instance variables. A static member belongs to the class and is shared by all objects.

No. Instance members are related to objects. Instance variables are allocated when an object is created. Static members are associated with class loading.

Instance initializer blocks run before the constructor body. During object creation, instance variables are initialized, instance initializer blocks execute, and then the constructor body executes.

Yes. Constructors commonly initialize instance variables when an object is created.

No. Instance variables belong to objects, so an object must be created before accessing them, unless access happens from another instance context of the same object.

Common examples include student name, roll number, employee id, salary, account number, product price, and customer address because these values are usually different for each object.

Common examples include showDetails(), calculateSalary(), displayStudent(), deposit(), withdraw(), and updateProfile() because these methods usually work with object-specific data.

The main benefit of instance members is that they allow each object to maintain its own state and behavior, which is a core idea of object-oriented programming.

An instance initializer block can initialize object data, but constructors are usually preferred for object-specific initialization. Instance initializer blocks are useful for common initialization code shared by multiple constructors.

Next step: Learn Java Static Keyword

🚀 Continue →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Instance-members | Language: Java

Question 1 of 42
Q1. What will be the output?
class Demo {
    {
        System.out.println("Instance block");
    }

    Demo() {
        System.out.println("Constructor");
    }

    public static void main(String[] args) {
        new Demo();
    }
}
Q2. What will happen in this code?
class Test {
    int number;

    void show() {
        System.out.println(number);
    }

    public static void main(String[] args) {
        Test t = new Test();
        t.show();
    }
}
Q3. Can a static method directly access an instance variable?
Q4. Where are instance variables declared?
Q5. What are instance members in Java?
Q6. Which of the following is an instance member?
Q7. Can an instance method directly access instance variables of the same class?
Q8. Which of the following is not an instance member?
Q9. Do all objects share the same copy of an instance variable?
Q10. Which access is preferred for instance variables in good object-oriented design?
Q11. Which statement is false about instance variables?
Q12. What is the main difference between instance variable and static variable?
Q13. Which statement is true?
Q14. What is an instance method?
Q15. What will be the output?
class Student {
    int marks = 50;

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

        s2.marks = 90;
        System.out.println(s1.marks);
    }
}
Q16. Which is the correct way to access an instance variable from a static method?
Q17. Can constructors access instance variables?
Q18. Which keyword is not used with instance members?
Q19. Can an instance method directly access static variables?
Q20. What will be the output?
class Student {
    String name;

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

        s1.name = "Ayan";
        s2.name = "Sarah";

        System.out.println(s1.name);
        System.out.println(s2.name);
    }
}
Q21. What will be the output?
class Student {
    int marks = 50;

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

        s1.marks = 90;
        System.out.println(s2.marks);
    }
}
Q22. Instance members belong to which of the following?
Q23. What is the default value of an instance variable of type int?
Q24. When does an instance initializer block execute?
Q25. What is an instance variable?
Q26. What is the default value of an instance variable of type String?
Q27. How do we usually call an instance method from main()?
Q28. What is the main difference between instance variable and local variable?
Q29. Which one is object-level behavior?
Q30. What will happen in this code?
class Test {
    int number = 10;

    public static void main(String[] args) {
        System.out.println(number);
    }
}
Q31. Do local variables get default values automatically like instance variables?
Q32. Which statement is true about instance initializer blocks?
Q33. What is an instance initializer block?
Q34. What will happen in this code?
class Test {
    int number = 10;

    public static void main(String[] args) {
        Test obj = null;
        System.out.println(obj.number);
    }
}
Q35. What is a correct use of instance members?
Q36. What will happen if we create 3 objects of a class with one instance initializer block?
Q37. Which member is shared by all objects?
Q38. What will happen in this code?
class Test {
    void show() {
        int number;
        System.out.println(number);
    }
}
Q39. What happens if an object reference is null and we access an instance variable?
Q40. Which member has a separate copy for every object?
Q41. What will be the output?
class Demo {
    {
        System.out.println("Block");
    }

    Demo() {
        System.out.println("Constructor");
    }

    public static void main(String[] args) {
        new Demo();
        new Demo();
    }
}
Q42. What will be the output?
class Student {
    String name = "Ayan";

    void showName() {
        System.out.println(name);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.showName();
    }
}

🎉 Great job! Continue learning Java OOP step by step.

Discussion

Ask questions, share suggestions, or discuss this lesson.

Please login or create an account to join the discussion and save your learning activity.

Loading comments...

Have you completed this lesson?

Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.

Not completed yet.