Be Concrete about Abstractions
Hsuan-Tien Lin
Deptartment of CSIE, NTU
OOP Class, March 24, 2009
Object Lifecycle (1/1)
1 c l a s s Record {
2 i n t sc or e ;
3 Record (i n t i n i t _ s c o r e ) { sc or e = i n i t _ s c o r e ; }
4 p r o t e c t e d v o i d f i n a l i z e ( ) t h r o w s Throwable { }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r ; / / r e f e r e n c e d e c l a r e d
9 Record r 2 ;/ / r e f e r e n c e d e c l a r e d
10 r = new Record ( 6 0 ) ; / / memory a l l o c a t e d (RHS)
11 / / and c o n s t r u c t o r c a l l e d
12 / / r e f e r e n c e assigned ( LHS )
13 r 2 = r ; / / r e f e r e n c e c o p i e d
14 r . s co re = 3 ; / / o b j e c t c o n t e n t accessed
15 r . show_score ( ) ; / / o b j e c t a c t i o n performed
16 r 2 = n u l l; r = n u l l;/ / memory s l o t i s o l a t e d
17 / / . . . .
18 / / f i n a l i z e r c a l l e d
19 / / o r JVM t e r m i n a t e d
20 }
21 }
Object Lifecycle: Key Point
we control birth, life, death, funeral design, but not the
exact funeral time (nor whether it would happen)
Class Lifecycle (1/1)
1 c l a s s Record {
2 s t a t i c{/ / c l a s s i n i t i a l i z e r
3 co unt = 0 ; / / a c t u a l l y , done by d e f a u l t
4 }
5 s t a t i c i n t cou nt ;
6 i n t sc or e ;
7 Record (i n t i n i t _ s c o r e ) { sc or e = i n i t _ s c o r e ; cou nt ++; }
8 p r o t e c t e d v o i d f i n a l i z e ( ) t h r o w s Throwable { }
9 / / c l a s s F i n a l i z e ? has been removed
10 }
11 / / t h e c l a s s RecordDemo s h o u l d be loaded a f t e r ’ j a v a RecordDemo ’
12 p u b l i c c l a s s RecordDemo {
13 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
14 / / t h e c l a s s Record s h o u l d be loaded ( i n i t i a l i z e d ) b e f o r e
15 / / t h e f o l l o w i n g l i n e s
16 Record r , r 2 ;
17 r = new Record ( 6 0 ) ;
18 r 2 = r ;
19 / / . . . .
20 / / by d e f a u l t , no unload ( and hence co unt would remain )
21 / / i f unloaded , no guarantees
22 }
23 }
Class Lifecycle: Key Point
automatic and dynamic loading/linking in JVM:
almost no need to care about load/unload
Method (1/2, Callee’s View)
1 c l a s s Record {
2 i n t sc or e ;
3 v o i d add_to (i n t i n c ) { s co re += i n c ; }
4 double g e t _ a d j u s t e d ( ) { r e t u r n (double) ( s co re + 3 0 ) ; }
5 }
method: what I (the object) do
parameter: what I get from the caller
return value: what I want to tell the caller
Method (2/2, Caller’s View)
1 c l a s s Record {
2 i n t sc or e ;
3 v o i d add_to (i n t i n c ) { s co re += i n c ; }
4 double g e t _ a d j u s t e d ( ) { r e t u r n (double) ( s co re + 3 0 ) ; }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r = new Record ( ) ;
9 r . addto ( 5 0 ) ;
10 System . o u t . p r i n t l n ( r . g e t _ a d j u s t e d ( ) ) ;
11 }
12 }
method: what I (caller) want the object to do argument: what I tell the callee
return value: what I want to hear from the callee
Method: Key Point
method: an abstraction of action, where informa-
tion is passed through argument/parameter and re-
turn values
Return Values (1/1)
1 c l a s s Record {
2 S t r i n g name ;
3 i n t sc or e ;
4 v o i d add_to (i n t i n c ) { s co re += i n c ; }
5 double g e t _ a d j u s t e d ( ) { r e t u r n ( s co re + 3 0 ) ; }
6 S t r i n g get_name ( ) { r e t u r n name ; }
7 }
8 p u b l i c c l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r = new Record ( ) ;
11 r . name = " HTLin " ;
12 System . o u t . p r i n t l n ( r . get_name ( ) == r . name ) ;
13 }
14 }
void: no return
primitive type: its value
reference type: its value (a.k.a. reference, not content)
Return Values: Key Point
Java: return by primitive/reference values
Primitive Argument/Parameter (1/1)
1 c l a s s Tool {
2 v o i d swap (i n t f i r s t , i n t second ) {
3 i n t tmp = f i r s t ;
4 f i r s t = second ;
5 second = tmp ;
6 System . o u t . p r i n t l n ( f i r s t ) ;
7 System . o u t . p r i n t l n ( second ) ;
8 }
9 }
10 p u b l i c c l a s s Demo{
11 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
12 Tool t = new Tool ( ) ;
13 i n t i = 3 ; i n t j = 5 ;
14 t . swap ( i , j ) ;
15 System . o u t . p r i n t l n ( i ) ;
16 System . o u t . p r i n t l n ( j ) ;
17 }
18 }
first, second: swapped
i, j: didn’t
Primitive Argument/Parameter: Key Point
argument ⇒ parameter: by value copying
–change in parameter does not change argument
Reference Argument/Parameter (1/3)
1 c l a s s Tool {
2 b o o l t r i c k y ( S t r i n g s1 , S t r i n g s2 ) {
3 s2 = s2 + " " ;
4 r e t u r n ( s1 == s2 ) ;
5 }
6 }
7 p u b l i c c l a s s Demo{
8 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
9 Tool t = new Tool ( ) ;
10 S t r i n g sa = " HTLin " ;
11 S t r i n g sb = sa ;
12 System . o u t . p r i n t l n ( t . t r i c k y ( sa , sb ) ) ;
13 System . o u t . p r i n t l n ( sa == sb ) ;
14 System . o u t . p r i n t l n ( t . t r i c k y ( sa + " " , sb ) ) ;
15 }
16 }
reference parameter passing: again, value copying sa, sb copied to s1, s2
s2 (reference) changed, sb didn’t
Reference Argument/Parameter (2/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt second ) {
4 i n t tmp = f i r s t . v a l ;
5 f i r s t . v a l = second . v a l ;
6 second . v a l = tmp ;
7 System . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
swapped as requested
Reference Argument/Parameter (3/3)
1 c l a s s m yI nt {i n t v a l ; m yI nt (i n t v ) { v a l = v ; } }
2 c l a s s Tool {
3 v o i d swap ( m yI nt f i r s t , m yI nt second ) {
4 m yI nt tmp = f i r s t ;
5 f i r s t = second ;
6 second = tmp ;
7 System . o u t . p r i n t l n ( f i r s t . v a l ) ;
8 System . o u t . p r i n t l n ( second . v a l ) ;
9 }
10 }
11 p u b l i c c l a s s Demo{
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 Tool t = new Tool ( ) ;
14 m yI nt i = new m yI nt ( 3 ) ;
15 m yI nt j = new m yI nt ( 5 ) ;
16 t . swap ( i , j ) ;
17 System . o u t . p r i n t l n ( i . v a l ) ;
18 System . o u t . p r i n t l n ( j . v a l ) ;
19 }
20 }
what happens?
Reference Argument/Parameter: Key Point
argument ⇒ parameter: by reference copying
–value change: does not change argument
–content change: change the “object”
Array Argument/Parameter (1/1)
1 c l a s s Tool {
2 v o i d swap (i n t[ ] both ) {
3 i n t tmp = both [ 0 ] ;
4 both [ 0 ] = both [ 1 ] ;
5 both [ 1 ] = tmp ;
6 }
7 }
8 p u b l i c c l a s s Demo{
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Tool t = new Tool ( ) ;
11 i n t[ ] a r r = new i n t[ 2 ] ;
12 a r r [ 0 ] = 3 ; a r r [ 1 ] = 5 ;
13 t . swap ( a r r ) ;
14 System . o u t . p r i n t l n ( a r r [ 0 ] ) ;
15 System . o u t . p r i n t l n ( a r r [ 1 ] ) ;
16 }
17 }
array is just special reference, same calling mechanism
Array Argument/Parameter: Key Point
argument ⇒ parameter: by reference value copying
–same as reference argument/parameter
Static Variables Revisited (1/1)
1 c l a s s Record {
2 s t a t i c i n t t o t a l _ r e c = 0 ;
3 i n t i d ;
4 p u b l i c Record ( ) { i d = t o t a l _ r e c + + ; }
5 }
6 p u b l i c c l a s s RecordDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 Record r 1 = new Record ( ) ;
9 Record r 2 = n u l l;
10 Record r 3 = new Record ( ) ;
11 System . o u t . p r i n t l n ( r 1 . t o t a l _ r e c ) ;
12 System . o u t . p r i n t l n ( r 2 . t o t a l _ r e c ) ;
13 System . o u t . p r i n t l n ( Record . t o t a l _ r e c ) ;
14 System . o u t . p r i n t l n ( r 1 . i d ) ;
15 System . o u t . p r i n t l n ( r 2 . i d ) ;
16 System . o u t . p r i n t l n ( Record . i d ) ;
17 }
18 }
r2.total_rec ⇒ Record.total_rec in compile time
Static Variables Revisited: Key Point
static variable:
of the class (shared), not of an object
Static Methods (1/2)
1 c l a s s myMath {
2 double mean (double a , double b ) {
3 r e t u r n ( a + b ) ∗ 0 . 5 ;
4 }
5 }
6 p u b l i c c l a s s MathDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 double i = 3 . 5 ;
9 double j = 2 . 4 ;
10 myMath m = new MyMath ( ) ;
11 System . o u t . p r i n t l n (m. mean ( i , j ) ) ;
12 }
13 }
new a myMath object just for computing mean
–lazy people don’t want to do so
Static Methods (2/2)
1 c l a s s myMath {
2 s t a t i c double mean (double a , double b ) {
3 r e t u r n ( a + b ) ∗ 0 . 5 ;
4 }
5 }
6 p u b l i c c l a s s MathDemo {
7 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
8 double i = 3 . 5 ;
9 double j = 2 . 4 ;
10 System . o u t . p r i n t l n ( myMath . mean ( i , j ) ) ;
11 System . o u t . p r i n t l n ( ( new myMath ( ) ) . mean ( i , j ) ) ;
12 }
13 }
make the method a static (class) one –no need to new an object
similar to static variable usage
Static Methods: Key Point
static method:
associated with the class,
no need to create an object
Use of Static Methods (1/3)
1 p u b l i c c l a s s UtilDemo {
2 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
3 System . o u t . p r i n t l n ( Math . PI ) ;
4 System . o u t . p r i n t l n ( Math . s q r t ( 2 . 0 ) ) ;
5 System . o u t . p r i n t l n ( Math . max ( 3 . 0 , 5 . 0 ) ) ;
6 System . o u t . p r i n t l n ( I n t e g e r . t o B i n a r y S t r i n g ( 1 5 ) ) ;
7 }
8 }
commonly used as utility functions
(so don’t need to create object)
Use of Static Methods (2/3)
1 c l a s s Record {
2 s t a t i c i n t t o t a l _ r e c = 0 ;
3 Record ( ) { t o t a l _ r e c ++; }
4 s t a t i c v o i d s h o w _ t o t a l _ r e c ( ) {
5 System . o u t . p r i n t l n ( t o t a l _ r e c ) ;
6 }
7 }
8 p u b l i c c l a s s RecordDemo {
9 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
10 Record r 1 = new Record ( ) ;
11 Record . s h o w _ t o t a l _ r e c ( ) ;
12 }
13 }
class related actions rather than object related actions
Use of Static Methods (3/3)
1 p u b l i c c l a s s MainDemo {
2 s t a t i c v o i d p r i n t S t r ( S t r i n g s ) {
3 System . o u t . p r i n t ( s ) ;
4 }
5 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
6 p r i n t S t r ( " I am main ; I am a l s o s t a t i c " ) ;
7 }
8 }
main is static (called by classname during ’java className’)
as tools for other static methods
Use of Static Methods: Key Point
static method:
compile time determined per class
sometimes useful
Instance versus Class (1/2)
1 c l a s s Record {
2 s t a t i c i n t cou nt ;
3 i n t i d ;
4 Record ( ) { i d = cou nt ++; }
5 s t a t i c v o i d s h o w _ i n t (i n t num ) {
6 System . o u t . p r i n t l n ( num ) ;
7 }
8 s t a t i c v o i d show_count ( ) {
9 s h o w _ i n t ( cou nt ) ;
10 }
11 v o i d show_id ( ) {
12 s h o w _ i n t ( i d ) ;
13 }
14 v o i d show_id_and_count ( ) {
15 show_id ( ) ;
16 show_count ( ) ;
17 }
18 }
instance methods: can access all members/methods
static methods: can access static members/methods
Instance versus Class (2/2)
1 / / t h i s : means my
2 c l a s s Record {
3 s t a t i c i n t cou nt ; i n t i d ;
4 Record ( ) { t h i s. i d = Record . cou nt ++; }
5 s t a t i c v o i d s h o w _ i n t (i n t num ) {
6 j a v a . l a n g . System . o u t . p r i n t l n ( num ) ;
7 }
8 s t a t i c v o i d show_count ( ) {
9 Record . s h o w _ i n t ( Record . cou nt ) ;
10 }
11 v o i d show_id ( ) {
12 Record . s h o w _ i n t (t h i s. i d ) ;
13 }
14 v o i d show_id_and_count ( ) {
15 t h i s. show_id ( ) ; Record . show_count ( ) ;
16 }
17 }
what compiler sees: a fully qualified intention
this: the “object” reference (known in run-time)
Record: the “class” name (known in compile-time)
Instance versus Class: Key Point
null/non-null instance access class method/variable: as if YES
class access instance method/variable method:
NO
instance method access class method/variable:
YES
class method access instance method/variable:
NO
this (1/3)
1 c l a s s Record {
2 s t a t i c i n t cou nt ; i n t i d ;
3 Record ( ) { t h i s. i d = Record . cou nt ++; }
4 s t a t i c v o i d s h o w _ i n t (i n t num ) {
5 j a v a . l a n g . System . o u t . p r i n t l n ( num ) ;
6 }
7 s t a t i c v o i d show_id ( Record r ) {
8 Record . s h o w _ i n t ( r . i d ) ;
9 }
10 v o i d o r i g _ s h o w _ i d ( ) {
11 Record . s h o w _ i n t (t h i s. i d ) ;
12 }
13 }
14 p u b l i c c l a s s RecordDemo {
15 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
16 Record r = new Record ( ) ;
17 r . o r i g _ s h o w _ i d ( ) ; Record . show_id ( r ) ;
18 }
19 }
a static implementation of show_id
“almost” what the compiler/JVM does
this (2/3)
1 c l a s s Record {
2 s t a t i c i n t cou nt ; i n t i d ;
3 Record ( ) { t h i s. i d = Record . cou nt ++; }
4 s t a t i c v o i d s h o w _ i n t (i n t num ) {
5 j a v a . l a n g . System . o u t . p r i n t l n ( num ) ;
6 }
7 s t a t i c v o i d show_id ( Record THIS ) {
8 Record . s h o w _ i n t ( THIS . i d ) ;
9 }
10 v o i d o r i g _ s h o w _ i d ( ) {
11 Record . s h o w _ i n t (t h i s. i d ) ;
12 }
13 }
14 p u b l i c c l a s s RecordDemo {
15 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
16 Record r = new Record ( ) ;
17 r . o r i g _ s h o w _ i d ( ) ; Record . show_id ( r ) ;
18 }
19 }
implicitly, instance method can access an additional
reference-type parameter this passed from the caller
this (3/3)
1 c l a s s Record {
2 i n t sc or e ;
3 s t a t i c v o i d show_score ;
4 v o i d s e t _ t o (i n t s co re ) { t h i s. sc or e = sc or e ; }
5 }
this: the reference to specifically say “my” variable/method
this: Key Point
this: the reference used to specifically say “mine”
—implicit in every instance method
Recursive Calls (1/5)
1 c l a s s D i r { F i l e [ ] f i l e s ; D i r [ ] d i r s ; }
2 c l a s s F i l e { S t r i n g name ; }
3 p u b l i c c l a s s DirDemo {
4 s t a t i c v o i d l i s t A l l ( D i r c u r r e n t ) {
5 i n t i , j ;
6 f o r( i = 0 ; i < f i l e s . l e n g t h ; i ++)
7 System . o u t . p r i n t l n ( f i l e s [ i ] . name ) ;
8 f o r( j = 0 ; j < d i r s . l e n g t h ; j ++)
9 l i s t A l l ( d i r s [ j ] ) ;
10 }
11 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
12 / / . . .
13 D i r d = new D i r ( ) ;
14 l i s t A l l ( d ) ;
15 }
16 }
recursive: when a method calls itself
Recursive Calls (2/5)
1 p u b l i c c l a s s FibDemo {
2 s t a t i c i n t f i b (i n t n ) {
3 i n t r e s ;
4 System . o u t . p r i n t l n ( " f i b ( " + n + " ) c a l l e d " ) ;
5 i f ( n <= 1 )
6 r e s = 1 ;
7 e l s e
8 r e s = f i b ( n−1) + f i b ( n −2);
9 System . o u t . p r i n t l n ( " f i b ( " + n + " ) r e t u r n i n g " ) ;
10 r e t u r n r e s ;
11 }
12 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
13 System . o u t . p r i n t l n ( f i b ( 5 ) ) ;
14 }
15 }
method call: do last task first (recursive or non-recursive)
method call stack: implement “last task first” (last-in-first-out)
Recursive Calls (3/5)
1 p u b l i c c l a s s FibDemo {
2 s t a t i c i n t f i b (i n t n ) {
3 i n t r e s ;
4 System . o u t . p r i n t l n ( " f i b ( " + n + " ) c a l l e d " ) ;
5 i f ( n <= 1 )
6 r e s = 1 ;
7 e l s e{
8 i n t m = n−1; r e s = f i b (m) ; r e s = r e s + f i b ( n −2);
9 }
10 System . o u t . p r i n t l n ( " f i b ( " + n + " ) r e t u r n i n g " ) ;
11 r e t u r n r e s ;
12 }
13 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) { i n t a = f i b ( 5 ) ; }
14 }
what needs to be stored in each “frame”?
my local version of n my local version of res
any temporary value generated by compiler (e.g. like m)
(my return value and where I am returning to)
Recursive Calls (4/5)
1 p u b l i c c l a s s F i b {
2 i n t N ;
3 F i b (i n t N ) { t h i s. N = N ; }
4 i n t g e t ( ) {
5 i f (N <= 1 )
6 r e t u r n 1 ;
7 e l s e
8 r e t u r n (new F i b ( N− 1 ) ) . g e t ( ) + (new F i b ( N− 2 ) ) . g e t ( ) ;
9 }
10 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
11 F i b f = new F i b ( 2 0 ) ;
12 System . o u t . p r i n t l n ( " r e s = " + f . g e t ( 5 ) ) ;
13 System . o u t . p r i n t l n ( " r e s = " + f . g e t ( 1 0 ) ) ;
14 }
15 }
one advanced use: one class, two behaviors utility behavior: static main
object behavior: with a state
is get recursive? seems No (different objects) but actually Yes
(same method with different “this”)
Recursive Calls (5/5)
1 p u b l i c c l a s s F i b {
2 i n t MAXN; i n t[ ] computed ;
3 F i b (i n t MAXN) {
4 t h i s.MAXN = MAXN; computed = new i n t[MAXN+ 1 ]
5 }
6 i n t g e t (i n t n ) {
7 i f ( computed [ n ] == 0 ) {
8 i f ( n <= 1 ) computed [ n ] = 1 ;
9 e l s e computed [ n ] = g e t ( n−1) + g e t ( n −2);
10 }
11 r e t u r n computed [ n ] ;
12 }
13 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
14 F i b f = new F i b ( 2 0 ) ;
15 System . o u t . p r i n t l n ( " r e s = " + f . g e t ( 5 ) ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . g e t ( 1 0 ) ) ;
17 }
18 }
recursive, but no repeated computation
no need to create so many objects
think: any better implementations?
Recursive Calls: Key Point
method call: not just “goto”
—comes with a frame of status passing, storing, ma-
nipulating, and returning
Local Variables (1/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 )
7 r e s = 1 ;
8 e l s e{
9 r e s = f i b ( n −1);
10 r e s = r e s + f i b ( n −2);
11 }
12 r e t u r n r e s ;
13 }
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
15 FibDemo f = new FibDemo ( ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
17 }
18 }
local primitive n: allocated, and assigned by argument ⇒
parameter
Local Variables (2/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 )
7 r e s = 1 ;
8 e l s e{
9 r e s = f i b ( n −1);
10 r e s = r e s + f i b ( n −2);
11 }
12 r e t u r n r e s ;
13 }
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
15 FibDemo f = new FibDemo ( ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
17 }
18 }
local referece this: allocated, and assigned by argument (f) ⇒
parameter (this)
Local Variables (3/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 )
7 r e s = 1 ;
8 e l s e{
9 r e s = f i b ( n −1);
10 r e s = r e s + f i b ( n −2);
11 }
12 r e t u r n r e s ;
13 }
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
15 FibDemo f = new FibDemo ( ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
17 }
18 }
local primitive res: allocated, not initialized assigned by ourselves
Local Variables (4/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 )
7 r e s = 1 ;
8 e l s e{
9 r e s = f i b ( n −1);
10 r e s = r e s + f i b ( n −2);
11 }
12 r e t u r n r e s ;
13 }
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
15 FibDemo f = new FibDemo ( ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
17 }
18 }
local reference s: reference allocated, not initialized, point to a
valid object by ourselves
Local Variables (5/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 )
7 r e s = 1 ;
8 e l s e{
9 r e s = f i b ( n −1);
10 r e s = r e s + f i b ( n −2);
11 }
12 r e t u r n r e s ;
13 }
14 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
15 FibDemo f = new FibDemo ( ) ;
16 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
17 }
18 }
some other local variables generated by compiler: allocated, not
initialized, used internally
Local Variables (6/7)
1 p u b l i c c l a s s FibDemo {
2 i n t f i b (i n t n ) {
3 i n t r e s ;
4 S t r i n g s = " f i b ( " + n + " ) c a l l e d " ;
5 System . o u t . p r i n t l n ( s ) ;
6 i f ( n <= 1 ) r e s = 1 ;
7 e l s e r e s = f i b ( n−1) + f i b ( n −2);
8 r e t u r n r e s ;
9 }
10 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] arg ) {
11 FibDemo f = new FibDemo ( ) ;
12 System . o u t . p r i n t l n ( " r e s = " + f . f i b ( 5 ) ) ;
13 }
14 }
when call returns: the result is “copied” to the previous frame somewhere
local primitive: simply discarded
local reference: discarded (and then the “object” GC’ed some time
if no longer used)
Local Variables (7/7, courtesy of Prof. Chuen-Liang Chen )
local variable
instance variable
class(static) variable
b l t method
i t l
belong to method
invocation instance class declaration within method within class within class
difi i O O S
modifier static NO NO YES
allocation (when) method invocation
instance creation
class loadingg allocation (where) stack memory heap memory heap memory
initial to 0 NO YES YES
de-allocation method return
automatic garbage collection
NO
scope
usage range direct access range from
declaration to whole class whole class
Chuen-Liang Chen, NTU CS&IE / 5 OOP
declaration to end of block
whole class whole class