OOP_2|Static Members in Java: Understanding and Using Them

Nikita pandey
4 min readFeb 19, 2023

Learn about static methods in Java and how they can be used in class-level operations. Understand the difference between static and non-static members, how to access them, and their use in initialization. Also, discover the basics of inheritance in Java and how it enables code reuse and polymorphism.

1. Static Members and Their Significance in Java

  • Static members can be accessed without creating any objects of the class
  • They can be declared for both methods and variables
  • The most common example of a static member is the main() method, which must be called before any objects exist

2. Static Methods in Java and Their Characteristics

  • A static method belongs to the class, not to the object
  • It can only access other static data, not non-static data (instance variables)
  • It can be accessed directly by the class name and doesn’t need any object
  • It cannot refer to “this” or “super” keywords in any way
  • It can only call other static methods and cannot call a non-static method from it.

3. Accessing Non-Static Members in a Static Context

  • Non-static members belong to an instance and cannot be accessed without resolving which instance of a class you are talking about
  • You can access non-static members in a static context by specifying the object reference explicitly.

4. Static Blocks in Java

  • Static blocks are used to do computations in order to initialize static variables
  • They are executed exactly once when the class is first loaded.

Example Code:

class UseStatic {
static int a = 3;
static int b;

static void meth(int x) {
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static {
System.out.println("Static block initialized.");
b = a * 4;
}

public static void main(String args[]) {
meth(42);
}
}

5. Static Inner Classes in Java

  • Only nested classes can be static
  • Static inner classes can have static variables

Example code:

public class Static {
static class Test{
String name;

public Test(String name) {
this.name = name;
}
}

public static void main(String[] args) {
Test a = new Test("Kunal");
Test b = new Test("Rahul");
System.out.println(a.name); // Kunal
System.out.println(b.name); // Rahul
}
}

6. Inheritance and Static Methods in Java

  • In Java, you cannot override inherited static methods
  • Overriding takes place by resolving the type of object at runtime, not compile time, and then calling the respective method
  • Static interface methods are not inherited by either an implementing class or a sub-interface
  • Let’s understand this topic more:
  • In Java, inheritance allows a subclass to inherit the characteristics of its superclass. This includes not only instance variables and instance methods but also static variables and static methods. Static methods in Java are methods that belong to the class rather than to an instance of the class.
  • Let’s start by looking at a simple example of inheritance in Java:
public class Animal {
public void makeSound() {
System.out.println("The animal makes a sound.");
}
}

public class Cat extends Animal {
public void makeSound() {
System.out.println("The cat meows.");
}
}

In this example, we have a superclass called Animal and a subclass called Cat. The Cat class inherits the makeSound() method from the Animal class, but overrides it to provide its own implementation.

Now, let’s take a look at how static methods work with inheritance in Java:

public class Shape {
public static void printType() {
System.out.println("This is a shape.");
}
}

public class Circle extends Shape {
public static void printType() {
System.out.println("This is a circle.");
}
}

In this example, we have a static method called printType() in the Shape class. The Circle class inherits this static method from the Shape class, but overrides it to provide its own implementation.

One thing to keep in mind with static methods in Java is that they can only access other static methods and variables. They cannot access non-static (instance) methods and variables. Additionally, static methods are resolved at compile-time, not at run-time like instance methods.

Here’s an example to illustrate this:

public class Example {
public static int staticVar = 1;
public int instanceVar = 2;

public static void printStatic() {
System.out.println("Static variable: " + staticVar);
// This line won't compile because instanceVar is not static
// System.out.println("Instance variable: " + instanceVar);
}

public void printInstance() {
System.out.println("Static variable: " + staticVar);
System.out.println("Instance variable: " + instanceVar);
}
}

In this example, we have a class called Example with a static variable called staticVar and an instance variable called instanceVar. We also have a static method called printStatic() and an instance method called printInstance().

Inside the printStatic() method, we cannot access the instanceVar variable because it is not static. However, inside the printInstance() method, we can access both the staticVar and instanceVar variables.

Photo by Roman Synkevych 🇺🇦 on Unsplash

Overall, static members in Java offer useful functionality and can be accessed without creating objects, making them valuable for certain use cases. Understanding their characteristics and proper usage can help in building more efficient and effective Java programs.

--

--

Nikita pandey

Python developer| Data science student| detective by nature| into art,AI, and books