In this post, we’ll focus on how methods interact with static and non-static members and why static local variables are not allowed. This is a common interview topic and a crucial concept for Java developers.
This post builds on the concepts covered in previous posts of this series. If you're new to the static keyword, I recommend checking out Static Block and Static Variables for a better foundation before diving into the topics discussed here.
Since static methods operate at the class level, they can’t access instance members directly.
package keywords.static_keyword; public class StaticVariables { // Static and non-static variables static int idStatic = 1; // Shared across all instances int id; // Unique to each instance String name; // Unique to each instance // Static block – Runs once when the class is loaded static { displayStatic(); // Call to static method // Cannot access non-static method in static block display(); // --> Compilation error } // Constructor to initialize non-static variables public StaticVariables(String name) { this.id = ++idStatic; // Increment static ID for every new instance this.name = name; } // Non-static method: Can access both static and non-static members void display() { System.out.println("Instance Method:"); System.out.println("Static ID: " + idStatic + ", Instance ID: " + id + ", Name: " + name); } // Static method: Can only access static members directly static void displayStatic() { System.out.println("Static Method:"); System.out.println("Static ID: " + idStatic); // Static local variables are not allowed static int localVar = 10; // --> Compilation error } public static void main(String[] args) { // Call static method directly displayStatic(); // Create instances to access non-static methods StaticVariables obj1 = new StaticVariables("Object1"); StaticVariables obj2 = new StaticVariables("Object2"); // Access non-static methods through objects obj1.display(); obj2.display(); } }
Java does not allow static local variables inside methods or blocks.
package keywords.static_keyword; public class StaticVariables { // Static and non-static variables static int idStatic = 1; // Shared across all instances int id; // Unique to each instance String name; // Unique to each instance // Static block – Runs once when the class is loaded static { displayStatic(); // Call to static method // Cannot access non-static method in static block display(); // --> Compilation error } // Constructor to initialize non-static variables public StaticVariables(String name) { this.id = ++idStatic; // Increment static ID for every new instance this.name = name; } // Non-static method: Can access both static and non-static members void display() { System.out.println("Instance Method:"); System.out.println("Static ID: " + idStatic + ", Instance ID: " + id + ", Name: " + name); } // Static method: Can only access static members directly static void displayStatic() { System.out.println("Static Method:"); System.out.println("Static ID: " + idStatic); // Static local variables are not allowed static int localVar = 10; // --> Compilation error } public static void main(String[] args) { // Call static method directly displayStatic(); // Create instances to access non-static methods StaticVariables obj1 = new StaticVariables("Object1"); StaticVariables obj2 = new StaticVariables("Object2"); // Access non-static methods through objects obj1.display(); obj2.display(); } }
static void displayStatic() { static int localVar = 10; // --> Compilation error }
|
Access Static Members | Access Non-Static Members | Allow Static Local Variables? | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Static Method | Yes | No | No | ||||||||||||||||
Non-Static Method | Yes | Yes | No | ||||||||||||||||
Static Block | Yes | No | No |
Static methods and members are essential tools in Java. Here are the key takeaways:
By understanding these rules, you'll be able to use static methods effectively in your programs.
Java Fundamentals
Array Interview Essentials
Java Memory Essentials
Collections Framework Essentials
Happy Coding!
The above is the detailed content of Static Keyword: Accessing Static and Non-Static Members in Methods. For more information, please follow other related articles on the PHP Chinese website!