0% found this document useful (0 votes)
2 views8 pages

Understanding Java Scope Rules

Uploaded by

jshehei
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)
2 views8 pages

Understanding Java Scope Rules

Uploaded by

jshehei
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

Java 5

Lecture 11: Java Scope Rule

What is Scope in Java?


Scope means the visibility and lifetime of a variable or method — where in your
program it can be accessed or used.

There are two main types of scope rules:

Static Scope (Lexical Scope) – Most commonly used in Java.

Dynamic Scope – Rare and not applicable in Java.

In Java, we follow static scoping.

1. Static Scope Rule


In static scope, the compiler decides what variables/methods refer to, based on
the program’s structure.

Example: Understanding Static Scope with Classes

class Box {
float x = 10, y = 20;
void area() {
[Link]("Box area: " + (x * y));
}
}

class Circle {
float x = 5, y = 5, r = 7;
void area() {
[Link]("Circle area: " + (3.14 * r * r));

Java 5 1
}
}

public class GeoClass {


float x = 50, y = 60;

public static void main(String[] args) {


Box b = new Box();
Circle c = new Circle();

[Link]("GeoClass x: " + new GeoClass().x); // 50


[Link]("Box x: " + b.x); // 10
[Link]("Circle x: " + c.x); // 5

[Link]();
[Link]();
}
}

Each class has its own version of x and y .

b.x , c.x , etc., access the variable from their own class.

2. Scope Inside Methods

public class ScopeExample {


public static void main(String[] args) {
int x = 20; // visible throughout main

if (x > 10) {
int y = 30; // y is visible only inside this if block
[Link](x + y);
}

Java 5 2
// [Link](y); // ❌ Error: y not visible here
}
}

x is declared in method → scope is entire method

y is declared in block → scope is only that block

3. Instance vs Class (Static) Variables


Instance Variable
Unique copy per object.

Declared without static .

Static/Class Variable
Shared across all objects.

Declared with static .

Example:

class Circle {
public double x, y, r;
static int circleCount = 0; // class variable

Circle(double x, double y, double r) {


this.x = x;
this.y = y;
this.r = r;
circleCount++; // shared across all objects
}
}

public class TestCircle {

Java 5 3
public static void main(String[] args) {
Circle c1 = new Circle(0, 0, 1);
Circle c2 = new Circle(0, 0, 2);
Circle c3 = new Circle(0, 0, 3);

[Link]([Link]); // prints 3
}
}

4. Static Methods (Class Methods)


Declared with static

Can be called without creating an object

class MathUtil {
static int square(int x) {
return x * x;
}
}

public class TestStaticMethod {


public static void main(String[] args) {
[Link]([Link](5)); // ✅ No object needed
}
}

5. Method Overloading + Static

class Circle {

Java 5 4
double r;

Circle(double r) {
this.r = r;
}

// Instance method
Circle bigger(Circle c) {
return this.r > c.r ? this : c;
}

// Static method
static Circle bigger(Circle a, Circle b) {
return a.r > b.r ? a : b;
}
}

[Link](b) → uses instance method

[Link](a, b) → uses static method

6. Nested Class
A nested class is a class defined within another class.

Example:

class Circle {
double x, y, r;

static class Point {


double px, py;

Point(double x, double y) {

Java 5 5
px = x;
py = y;
}

void display() {
[Link]("(" + px + ", " + py + ")");
}
}

boolean isInside(Point p) {
double distance = [Link]([Link]([Link] - x, 2) + [Link]([Link] - y,
2));
return distance <= r;
}
}

public class Test {


public static void main(String[] args) {
Circle c = new Circle();
c.x = 0;
c.y = 0;
c.r = 5;

[Link] p = new [Link](3, 4);


[Link]();

[Link]("Inside Circle: " + [Link](p)); // true


}
}

7. Recursion in Java

Factorial (Recursive)

Java 5 6
class Factorial {
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

public static void main(String[] args) {


Factorial f = new Factorial();
[Link]([Link](5)); // 120
}
}

Fibonacci (Recursive)

class Fibonacci {
int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}

public static void main(String[] args) {


Fibonacci f = new Fibonacci();
for (int i = 0; i < 10; i++)
[Link]([Link](i) + " ");
}
}

GCD (Recursive)

Java 5 7
class GCD {
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

public static void main(String[] args) {


GCD g = new GCD();
[Link]([Link](48, 18)); // 6
}
}

Java 5 8

You might also like