Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In Java, the scope of a variable refers to the part of the code where the variable is accessible. Understanding the scope of different types of variables is essential for writing efficient and maintainable code. This tutorial will discuss the scope of static variables, global variables (instance variables), and local variables in Java.
Static variables, also known as class variables, are declared inside a class but outside any method, constructor, or block with the static
keyword. There is only one copy of a class variable shared among all instances of the class. The scope of a static variable is the entire class in which it is declared.
Example:
public class MyClass { static int classVar; public void displayClassVar() { System.out.println("Class variable value: " + classVar); } public static void main(String[] args) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(); obj1.classVar = 10; obj1.displayClassVar(); // Output: Class variable value: 10 obj2.classVar = 20; obj2.displayClassVar(); // Output: Class variable value: 20 obj1.displayClassVar(); // Output: Class variable value: 20 } }
Global variables, also known as instance variables, are declared inside a class but outside any method, constructor, or block. Each instance of the class has its own copy of the instance variables. The scope of an instance variable is the entire class, but it can only be accessed through an instance of the class.
Example:
public class MyClass { int instanceVar; public void displayInstanceVar() { System.out.println("Instance variable value: " + instanceVar); } public static void main(String[] args) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(); obj1.instanceVar = 10; obj1.displayInstanceVar(); // Output: Instance variable value: 10 obj2.instanceVar = 20; obj2.displayInstanceVar(); // Output: Instance variable value: 20 obj1.displayInstanceVar(); // Output: Instance variable value: 10 } }
Local variables are declared and used within a method, constructor, or block. They are created when the method or block is entered and destroyed when it is exited. The scope of a local variable is limited to the method, constructor, or block in which it is declared.
Example:
public class MyClass { public void myMethod() { int localVar = 10; System.out.println("Local variable value: " + localVar); } public static void main(String[] args) { MyClass obj = new MyClass(); obj.myMethod(); // Output: Local variable value: 10 // localVar cannot be accessed here since it is out of scope } }
In conclusion, understanding the scope of different types of variables in Java is crucial for efficient and maintainable code. Static variables have a scope of the entire class and are shared among all instances of the class, global variables (instance variables) have a scope of the entire class but can only be accessed through an instance of the class, and local variables have a limited scope within the method, constructor, or block they are declared in.
Global variables in Java and their scope: In Java, global variables are typically referred to as class-level variables (instance or static). They have class-wide scope.
public class MyClass { int instanceVar; // Instance variable static int staticVar; // Static (class-level) variable }
Local variable scope in Java methods: Local variables in Java methods have method-wide scope and are only accessible within the method.
public class MyClass { void myMethod() { int localVar; // Local variable // localVar is only accessible within myMethod } }
Java static vs instance variable scope: Static variables have class-wide scope, while instance variables have object-wide scope.
public class MyClass { int instanceVar; // Instance variable static int staticVar; // Static (class-level) variable }
Class-level variables and their scope in Java: Class-level variables include both instance and static variables, providing different scopes.
public class MyClass { int instanceVar; // Instance variable static int staticVar; // Static (class-level) variable }
Java global variables in different classes: Global variables in Java can be accessed across different classes when they are declared as static variables.
public class AnotherClass { void accessGlobal() { int globalVar = MyClass.staticVar; // Accessing static variable from another class } }
Block-level scope for local variables in Java: Local variables declared within a block have block-level scope and are only accessible within that block.
public class MyClass { void myMethod() { if (someCondition) { int blockVar; // Block-level variable // blockVar is only accessible within this block } } }
Visibility modifiers and variable scope in Java:
Visibility modifiers (public
, private
, protected
, package-private) control the access scope of variables.
public class MyClass { private int privateVar; // Private variable public int publicVar; // Public variable }
Accessing global variables in Java methods: Global (class-level) variables can be accessed within methods of the same class.
public class MyClass { int globalVar; // Global variable void myMethod() { // Accessing globalVar within the method int localVar = globalVar; } }
Java static block and variable initialization: Static variables can be initialized using a static block, ensuring they are set before any method or constructor is called.
public class MyClass { static int staticVar; static { // Static block for initialization staticVar = 42; } }
Using static variables across classes in Java: Static variables can be shared across different classes by referencing the class name.
public class AnotherClass { void accessStatic() { int value = MyClass.staticVar; // Accessing static variable from another class } }
Static vs instance methods and variable scope in Java: Static methods and variables belong to the class, while instance methods and variables belong to an object. They have different scopes.
public class MyClass { static int staticVar; // Static variable int instanceVar; // Instance variable static void staticMethod() { // Accessing staticVar but not instanceVar } void instanceMethod() { // Accessing instanceVar but not staticVar } }
Java variable shadowing and scope resolution: Variable shadowing occurs when a local variable has the same name as an instance or static variable, impacting the scope resolution.
public class MyClass { int variable = 5; void myMethod() { int variable = 10; // Local variable shadowing instance variable // Accessing local variable: variable // Accessing instance variable: this.variable } }