⏱️ 7 min read • Beginner Level • Lesson 45
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.
A Java class contains different parts that define the data and behavior of objects. These parts are called class members.
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 ├── Variables │ ├── Instance Variable │ ├── Static Variable │ └── Local Variable ├── Methods ├── Constructors └── Initializer Blocks
Java class variables can be divided into three main types:
static keyword.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.
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
name is an instance variable, each object has its own copy.
Static variables are declared using the static keyword.
They belong to the class, not to individual objects. All objects share the same static variable.
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 are declared inside a method, constructor, or block. They can be used only inside the area where they are declared.
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
static.
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. |
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
A constructor is a special block that is called automatically when an object is created. It is used to initialize an object.
Student s1 = new Student();, the constructor of
Student is called automatically.
You will learn constructors in detail in the next lesson.
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:
Runs every time an object is created. It is used to initialize instance data.
Runs only once when the class is loaded. It is used to initialize static data.
An instance initializer block is written inside a class without the
static keyword. It executes every time an object of the class is created.
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
number is an instance variable.100 to number.obj1 is created, the initializer block executes first.obj2 is created, the initializer block executes again.
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.
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
course is a static variable."Java Programming" to course.| 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. |
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
name is an instance variable.
Each object gets its own copy of this variable.
course is a static variable.
It belongs to the class and is shared by all objects.
name.
rollNumber is a local variable
declared inside the showStudent() method.
showStudent() is an instance method
and is called using an object reference.
showCourse() is a static method
and is called using the class name.
static keyword to declare static members.static.🧠 Test your understanding with a quick quiz
Topic: Class-members | Language: Java
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);
}
}class Counter {
static int count = 0;
Counter() {
count++;
}
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
System.out.println(count);
}
}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();
}
}🎉 Great job! Continue learning Java OOP step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.