“Understanding Static and Non-static Members in Java”-(Java No-14)
3 min readSep 22, 2024
In Java, the keyword static is used to define class-level components, while non-static components are instance-level. Here's the distinction:
1. Static:
- Belongs to the class: Static members (methods, variables, inner classes) belong to the class itself, not to any instance (object) of the class. This means they are shared across all instances of the class.
- Access without creating an object: You can access static members directly through the class name, without creating an instance of the class.
- Memory efficiency: Since static variables are class-level, they are created once per class (in memory), rather than for every object.
- Use case: Static is used for constants, utility methods, and resources that should be shared across instances.
Example:
class MyClass {
static int staticVar = 10;
static void staticMethod() {
System.out.println("This is a static method.");
}
}
public class Test {
public static void main(String[] args) {
// Accessing static members
MyClass.staticMethod(); // No need to create an object
System.out.println(MyClass.staticVar);
}
}Output:
This is a static method.
102. Non-static (Instance):
- Belongs to the object: Non-static members (methods, variables) are specific to each object created from the class. Every object has its own copy of these members.
- Access requires an object: To access non-static members, you must first create an instance of the class.
- Memory usage: Non-static members are created separately for each object, meaning each instance can have its own unique values.
- Use case: Non-static is used when you need distinct data for each instance of the class.
Example:
class MyClass {
int nonStaticVar = 5;
void nonStaticMethod() {
System.out.println("This is a non-static method.");
}
}
public class Test {
public static void main(String[] args) {
// Creating an instance of the class
MyClass obj = new MyClass();
// Accessing non-static members
obj.nonStaticMethod();
System.out.println(obj.nonStaticVar);
}
}Output:
This is a non-static method.
5Get Gondi’s stories in your inbox
Join Medium for free to get updates from this writer.
Example 1: Static Variables vs Non-static Variables
class BankAccount {
static double interestRate = 3.5; // Static variable shared by all accounts
double balance; // Non-static variable specific to each account
// Constructor to initialize balance
BankAccount(double initialBalance) {
this.balance = initialBalance;
}
// Non-static method to display the account details
void displayAccountDetails() {
System.out.println("Balance: " + this.balance);
System.out.println("Interest Rate: " + interestRate);
}
}
public class Test {
public static void main(String[] args) {
// Creating two BankAccount objects
BankAccount account1 = new BankAccount(1000.0);
BankAccount account2 = new BankAccount(5000.0);
// Display details for both accounts
account1.displayAccountDetails();
account2.displayAccountDetails();
// Changing the static interestRate
BankAccount.interestRate = 4.0; // Changing for all accounts
// Display details again
System.out.println("\nAfter changing interest rate:\n");
account1.displayAccountDetails();
account2.displayAccountDetails();
}
}Output:
Balance: 1000.0
Interest Rate: 3.5
Balance: 5000.0
Interest Rate: 3.5
After changing interest rate:
Balance: 1000.0
Interest Rate: 4.0
Balance: 5000.0
Interest Rate: 4.0Explanation:
interestRateis a static variable. It’s shared across all instances ofBankAccount. When it is changed for one account, the change is reflected in all accounts.balanceis a non-static variable. EachBankAccountinstance has its own balance, and changing one instance’s balance does not affect the other.
Example 2: Static Method vs Non-static Method
class Calculator {
// Static method: belongs to the class, can be called without an object
static int add(int a, int b) {
return a + b;
}
// Non-static method: requires an instance to be called
int multiply(int a, int b) {
return a * b;
}
}
public class Test {
public static void main(String[] args) {
// Calling the static method without creating an object
int sum = Calculator.add(5, 10);
System.out.println("Sum: " + sum);
// To call the non-static method, we need an instance of Calculator
Calculator calc = new Calculator();
int product = calc.multiply(5, 10);
System.out.println("Product: " + product);
}
}Output:
Sum: 15
Product: 50Explanation:
addis a static method, so it can be accessed using the class nameCalculator.add(...)without creating an instance of theCalculatorclass.multiplyis a non-static method, meaning it can only be accessed through an instance of theCalculatorclass (i.e.,calc.multiply(...)).
When to Use Static vs Non-static:
- Static:
- Use
staticwhen the behavior or property is related to the class as a whole and not to any particular object. - Ideal for utility methods like mathematical operations (e.g.,
Math.max()), constants (e.g.,PI), or shared resources (e.g., logging).
2. Non-static:
- Use non-static when behavior or properties depend on the specific instance of a class, such as an individual bank account’s balance, or when different objects need to have their own separate data.



