0% found this document useful (0 votes)
17 views5 pages

9-Variables and Methods - Scopes

The document explains the different scopes of variables and methods in Java, which include Instance Scope, Static Scope, Local Scope, and Block Scope. Each scope has specific characteristics regarding where it is declared, how it can be accessed, and its lifetime. Examples are provided for each scope to illustrate their usage and behavior in Java programming.

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views5 pages

9-Variables and Methods - Scopes

The document explains the different scopes of variables and methods in Java, which include Instance Scope, Static Scope, Local Scope, and Block Scope. Each scope has specific characteristics regarding where it is declared, how it can be accessed, and its lifetime. Examples are provided for each scope to illustrate their usage and behavior in Java programming.

Uploaded by

Tanya Verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

VARIABLES AND METHODS’ SCOPES

In Java, variables and methods have different scopes depending on where and how they are
declared. The four main scopes are:
1. Instance Scope (Object-level)
2. Static Scope (Class-level)
3. Local Scope (Method-level)
4. Block Scope (Inside Loops/Blocks)

1. Instance Scope (Object-Level)


• Defined inside a class but outside methods.
• Belongs to an object (each object gets its own copy).
• Accessible through an object reference.
Example:
class Example {
int instanceVar = 10; // Instance variable

void show() {
System.out.println("Instance Variable: " + instanceVar);
}
}

public class Main {


public static void main(String[] args) {
Example obj1 = new Example();
Example obj2 = new Example();

obj1.instanceVar = 20; // Changes only for obj1


System.out.println(obj1.instanceVar); // 20
System.out.println(obj2.instanceVar); // 10 (remains unchanged)
}
}

2. Static Scope (Class-Level)


• Defined using static keyword inside a class but outside methods.
• Belongs to the class (shared across all objects).
• Accessible using the class name (or through an instance).
Example:
class Example {
static int staticVar = 100; // Shared by all objects (class variable)
int instanceVar = 50; // Separate for each object (instance variable)

void modifyVariables(int staticValue, int instanceValue) {


staticVar = staticValue; // Modifies shared static variable
instanceVar = instanceValue; // Modifies instance variable (only for that object)
}

void show() {
System.out.println("Instance Variable: " + instanceVar + ", Static Variable: " +
staticVar);
}

static void staticMethod() {


System.out.println("Static Method: Static Variable = " + staticVar);
}
}

public class Main {


public static void main(String[] args) {
Example obj1 = new Example();
Example obj2 = new Example();
System.out.println("Initial Values:");
obj1.show(); // Instance Variable: 50, Static Variable: 100
obj2.show(); // Instance Variable: 50, Static Variable: 100

// Modify instance and static variables using obj1


obj1.modifyVariables(200, 75);
System.out.println("\nAfter obj1 modifies values:");
obj1.show(); // Instance Variable: 75, Static Variable: 200
obj2.show(); // Instance Variable: 50, Static Variable: 200 (Static changed for all)

// Modify instance and static variables using obj2


obj2.modifyVariables(300, 90);
System.out.println("\nAfter obj2 modifies values:");
obj1.show(); // Instance Variable: 75, Static Variable: 300 (Static changed for all)
obj2.show(); // Instance Variable: 90, Static Variable: 300

// Calling Static Method


System.out.println("\nCalling Static Method:");
Example.staticMethod(); // Static Method: Static Variable = 300
}
}
OUTPUT
Initial Values:
Instance Variable: 50, Static Variable: 100
Instance Variable: 50, Static Variable: 100

After obj1 modifies values:


Instance Variable: 75, Static Variable: 200
Instance Variable: 50, Static Variable: 200
After obj2 modifies values:
Instance Variable: 75, Static Variable: 300
Instance Variable: 90, Static Variable: 300

Calling Static Method:


Static Method: Static Variable = 300

3. Local Scope (Method-Level)


• Declared inside a method.
• Exists only during method execution.
• Cannot be accessed outside the method.
Example:
class Example {
void display() {
int localVar = 50; // Local variable
System.out.println("Local Variable: " + localVar);
}
}

public class Main {


public static void main(String[] args) {
Example obj = new Example();
obj.display();

// System.out.println(localVar); // Error! Not accessible outside the method


}
}

4. Block Scope (Inside Loops/Blocks)


• Declared inside loops, if-statements, or blocks {}.
• Exists only inside the block.
• Cannot be accessed outside the block.
Example:
class Example {
void checkScope() {
if (true) {
int blockVar = 30; // Block-scoped variable
System.out.println("Inside Block: " + blockVar);
}
// System.out.println(blockVar); // Error! Not accessible outside the block
}
}

public class Main {


public static void main(String[] args) {
Example obj = new Example();
obj.checkScope();
}
}

Summary Table

Scope Declared Inside Accessed In Lifetime Keyword

Class (outside Inside instance methods (via As long as the object


Instance None
methods) object) exists

Class (outside Inside static/instance methods As long as the program


Static static
methods) (via class name) runs

Until the method


Local Inside a method Only within that method None
completes execution

Until the block


Block Inside a block {} Only within that block None
execution completes

You might also like