Specifications

Sun Services
Java™ Programming Language
Module 4, slide 6 of 31
Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision F
Variable Scope Example
public class ScopeExample {
private int i=1;
public void firstMethod() {
int i=4, j=5;
this.i = i + j;
secondMethod(7);
}
public void secondMethod(int i) {
int j=8;
this.i = i + j;
}
}
public class TestScoping {
public static void main(String[] args) {
ScopeExample scope = new ScopeExample();
scope.firstMethod();
}
}
main
firstMethod
i
j
secondMethod
i
j
this
this
scope
Heap Memory
ScopeExample
i
1
4
5
7
8
Execution Stack