Java Class Members - Fields, Methods, Constructors and Blocks

⏱️ 7 min read • Beginner Level • Lesson 45

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

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

In Java, a class member is anything declared inside a class. Class members mainly include fields, methods, constructors, and initializer blocks.

Before learning class members, you should understand Java class and objects, Java variables, and Java methods.


What are Class Members in Java?

A Java class contains different parts that define the data and behavior of objects. These parts are called class members.

Simple Meaning: Anything declared inside a class, such as variables, methods, constructors, and initializer blocks, is considered a class member.

Types of Class Members in Java

A Java class can contain the following components:

Class Member Meaning
Fields / Attributes Variables declared inside a class to store object or class data.
Methods Functions declared inside a class to define behavior.
Constructors Special blocks used to initialize newly created objects.
Initializer Blocks Blocks used to initialize objects or static data.
Class Structure Overview
Class
├── Variables
│   ├── Instance Variable
│   ├── Static Variable
│   └── Local Variable
├── Methods
├── Constructors
└── Initializer Blocks

Variables in a Java Class

Java class variables can be divided into three main types:

  • Instance variables: non-static variables declared inside a class.
  • Static variables: variables declared with the static keyword.
  • Local variables: variables declared inside a method, constructor, or block.

Instance Variables

Instance variables are non-static variables declared inside a class but outside any method, constructor, or block. Each object gets its own copy of instance variables.

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
Note: Since name is an instance variable, each object has its own copy.

Static Variables

Static variables are declared using the static keyword. They belong to the class, not to individual objects. All objects share the same static variable.

StaticVariableExample.java
Copy Try Download
class Counter {
    static int count = 0; // static variable

    Counter() {
        count++;
    }

    public static void main(String[] args) {
        new Counter();
        new Counter();
        new Counter();

        System.out.println("Total objects: " + count);
    }
}

Output:

Total objects: 3

Local Variables

Local variables are declared inside a method, constructor, or block. They can be used only inside the area where they are declared.

LocalVariableExample.java
Copy Try Download
class LocalVariableExample {
    void show() {
        int number = 10; // local variable
        System.out.println(number);
    }

    public static void main(String[] args) {
        LocalVariableExample obj = new LocalVariableExample();
        obj.show();
    }
}

Output:

10
Important: Local variables cannot be declared as static.

Methods in a Java Class

Methods define the behavior of a class. In Java, methods can be instance methods or static methods.

Method Type Meaning How to call
Instance Method Belongs to object. Called using object reference.
Static Method Belongs to class. Can be called using class name.
MethodMemberExample.java
Copy Try Download
class MethodMemberExample {
    void instanceMethod() {
        System.out.println("Instance method called");
    }

    static void staticMethod() {
        System.out.println("Static method called");
    }

    public static void main(String[] args) {
        MethodMemberExample obj = new MethodMemberExample();

        obj.instanceMethod();
        MethodMemberExample.staticMethod();
    }
}

Output:

Instance method called
Static method called

Constructor in Java

A constructor is a special block that is called automatically when an object is created. It is used to initialize an object.

Example: When you write Student s1 = new Student();, the constructor of Student is called automatically.

You will learn constructors in detail in the next lesson.

Initializer Blocks in Java

An initializer block in Java is a block of code written inside a class that is used to initialize data. Initializer blocks are executed automatically by Java.

Initializer blocks are mainly of two types:

1. Instance Initializer Block

Runs every time an object is created. It is used to initialize instance data.

2. Static Initializer Block

Runs only once when the class is loaded. It is used to initialize static data.

Simple Meaning: Initializer blocks are useful when you want some initialization code to run automatically before or during object creation.

Instance Initializer Block

An instance initializer block is written inside a class without the static keyword. It executes every time an object of the class is created.

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

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

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

    public static void main(String[] args) {
        InstanceInitializerExample obj1 = new InstanceInitializerExample();
        System.out.println("Number: " + obj1.number);

        InstanceInitializerExample obj2 = new InstanceInitializerExample();
        System.out.println("Number: " + obj2.number);
    }
}

Output:

Instance initializer block executed
Constructor executed
Number: 100
Instance initializer block executed
Constructor executed
Number: 100

Explanation

  1. The variable number is an instance variable.
  2. The instance initializer block assigns 100 to number.
  3. When obj1 is created, the initializer block executes first.
  4. After that, the constructor executes.
  5. When obj2 is created, the initializer block executes again.
  6. This proves that an instance initializer block runs every time an object is created.
Execution Order: For object creation, Java executes instance initializer block before the constructor body.

Static Initializer Block

A static initializer block is declared using the static keyword. It executes only once when the class is loaded into memory.

Static blocks are generally used to initialize static variables or perform one-time setup work.

StaticInitializerExample.java
Copy Try Download
class StaticInitializerExample {
    static String course;

    static {
        course = "Java Programming";
        System.out.println("Static initializer block executed");
    }

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

    public static void main(String[] args) {
        StaticInitializerExample obj1 = new StaticInitializerExample();
        StaticInitializerExample obj2 = new StaticInitializerExample();

        System.out.println("Course: " + course);
    }
}

Output:

Static initializer block executed
Constructor executed
Constructor executed
Course: Java Programming

Explanation

  1. The variable course is a static variable.
  2. The static initializer block assigns "Java Programming" to course.
  3. The static block executes only once when the class is loaded.
  4. The constructor executes every time an object is created.
  5. Since two objects are created, the constructor runs two times.
  6. The static initializer block runs only once.

Instance Initializer Block vs Static Initializer Block

Instance Initializer Block Static Initializer Block
Runs every time an object is created. Runs only once when the class is loaded.
Used to initialize instance variables. Used to initialize static variables.
Written without static keyword. Written with static keyword.
Executes before constructor body. Executes before object creation and before constructor execution.
Important: Both initializer blocks and constructors are executed during object creation. The initializer block runs first and is commonly used for shared initialization code, while the constructor runs afterward to perform object-specific initialization.

Complete Example of Class Members

ClassMembersExample.java
Copy Try Download
class Student {

    // Instance Variable
    String name;

    // Static Variable
    static String course = "Java Programming";

    // Instance Initializer Block
    {
        System.out.println("Instance Initializer Block Executed");
    }

    // Constructor
    Student(String studentName) {
        name = studentName;
    }

    // Instance Method
    void showStudent() {

        // Local Variable
        int rollNumber = 101;

        System.out.println("Name : " + name);
        System.out.println("Roll Number : " + rollNumber);
    }

    // Static Method
    static void showCourse() {
        System.out.println("Course : " + course);
    }

    public static void main(String[] args) {

        Student s1 = new Student("Ayan");

        s1.showStudent();

        Student.showCourse();
    }
}

Output:

Instance Initializer Block Executed
Name : Ayan
Roll Number : 101
Course : Java Programming

Explanation

  1. name is an instance variable. Each object gets its own copy of this variable.
  2. course is a static variable. It belongs to the class and is shared by all objects.
  3. The instance initializer block executes automatically whenever an object is created.
  4. The constructor initializes the instance variable name.
  5. rollNumber is a local variable declared inside the showStudent() method.
  6. showStudent() is an instance method and is called using an object reference.
  7. showCourse() is a static method and is called using the class name.
  8. This example demonstrates the major class members: instance variable, static variable, local variable, constructor, initializer block, instance method, and static method.

Points to Remember

  • Variables and methods can be static or non-static.
  • By default, class members are non-static.
  • Use the static keyword to declare static members.
  • Non-static members are called instance members.
  • Static members are also called class-level members because they belong to the class rather than individual objects.
  • Local variables cannot be static.

Common Mistakes

  • Trying to declare a local variable as static.
  • Calling instance methods directly from a static method without creating an object.
  • Confusing instance variables with local variables.
  • Thinking all class members are static by default.
  • Changing a static variable and forgetting that it is shared by all objects.
Summary:
  • Class members are components declared inside a Java class.
  • Class members include fields, methods, constructors, and initializer blocks.
  • Variables can be instance, static, or local variables.
  • Methods can be instance methods or static methods.
  • Instance members belong to objects.
  • Static members belong to the class.
  • Local variables cannot be static.

Interview Questions ⭐

Class members in Java are components declared inside a class, such as fields, methods, constructors, and initializer blocks.

Fields are variables declared inside a class. They represent the data or properties of objects or the class.

An instance variable is a non-static variable declared inside a class but outside any method, constructor, or block. Each object gets its own copy.

A static variable is declared using the static keyword and belongs to the class. It is shared by all objects of that class.

A local variable is declared inside a method, constructor, or block and can be used only within that area.

No, local variables cannot be declared as static in Java.

An instance method is a non-static method that belongs to an object and is called using an object reference.

A static method belongs to the class and can be called using the class name without creating an object.

An initializer block is a block of code inside a class used to initialize data. Java supports instance initializer blocks and static initializer blocks.

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

A static initializer block is declared using the static keyword. It runs only once when the class is loaded and is commonly used to initialize static variables.

A constructor initializes an object and can accept parameters, while an initializer block is used for common initialization logic. Instance initializer blocks run before the constructor body.

Yes, static blocks execute when the class is loaded, before any object is created and before constructors run.

Instance members belong to objects, while static members belong to the class.

Next step: Learn Java Instance Members

🚀 Continue to Java Instance Members →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Class-members | Language: Java

Question 1 of 10
Q1. What will be the output of this code?
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);
    }
}
Q2. What is an instance variable in Java?
Q3. What are class members in Java?
Q4. What is an instance initializer block?
Q5. Can a local variable be declared as static in Java?
Q6. What will be the output of this code?
class Counter {
    static int count = 0;

    Counter() {
        count++;
    }

    public static void main(String[] args) {
        new Counter();
        new Counter();
        new Counter();

        System.out.println(count);
    }
}
Q7. What will be the output of this code?
class Demo {
    static {
        System.out.println("Static block");
    }

    {
        System.out.println("Instance block");
    }

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

    public static void main(String[] args) {
        new Demo();
        new Demo();
    }
}
Q8. What is a static variable in Java?
Q9. What is a static initializer block?
Q10. Which of the following is NOT usually considered a class member?

🎉 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.