Professional Documents
Culture Documents
! " #
Supplementary Objectives
"
$ # " "
% # " "
" #
new
*
+ % )
, -./0 * # %
#
1
Reachable References
2 # 3
2 #
2
, -./0
+ # #
%# )
45 ) 1
% )
45% #
, 6 70 2 2
& # %
88 9:9
#
; % # #
< % % # +
% %
2 # " = :9 + # #
,9 .
0 #
# #
# %#
2
2 %
! %
2
2
2 + # >
2
# % 2
&
%
*# %
= = :9 # o4%o5%o11%o12%o14% o15
= # %
%
2 o1%o2% o3 %
% < % o5%o6% o7
% % %05% <
o8%o10%o11% o9 , 0
%
< o9 o11 % o11 <
o8 o10
#
? % #
# #
%# " , -.@0
) %
"
+ %
? ,
0 2
# # % #
# # # #
# ;
java.lang.ref.Reference ,
SoftReference%
WeakReference%PhantomReference0
, 02
%
#
%#
6 % %
# + % finally
try-catch-finally , 8/% 9::0
% # # % #
" %
<
# %
%
% # *# %
# +
% %
import java.io.*;
class WellbehavedClass {
// ...
void wellbehavedMethod() {
File aFile;
long[] bigArray = new long[20000];
+ % null ,
90 ,
.0%
# %
" A #
+ # %
!
%
& :9 # 6
HeavyItem # % # # #
& HeavyItem
finalize() Object +
$# "
# , " % -.@0
# HeavyItem
% HeavyItem
C 3
main() # ,
9B0 ,9/0%
itemOne itemTwo% itemOne ,
9:0% HeavyItem
# One # createHeavyItem()
,970 ,.D0 HeavyItem %# %
< Y,
list0 HeavyItem # Two , itemTwo0
1 # main() 2 #
HeavyItem # Four Five # %
,
970 ,
.D0
% 2
Object Finalization
< "
finalize()
finalize()
Object
2 finalize() 2
" Object
0 # %
" #
+ # % " ,
90# #
%
" # % "
Finalizer Chaining
? % " , B-%
.@-0 % " "
% # ,@0 "
finally ,
-0% #
try ,.0
2 " , % 0
%
< A this % #
" %
+ # %
% " #
% "
& :. " + 1
1 " " 1
,
/0 main() Blob %
< Blob ,
-0 Blob
,@0 " fat Blob
BasicBlob A ,
blobId0
,population0 6 Blob
,@0 +$ "Hello" finalize()
,
80 Blob + "Bye"
finalize() BasicBlob ,
.0%#
# # #
,:0# + "Bye"
# ,
/0
BasicBlob() {
blobId = idCounter++;
++population;
}
protected void finalize() throws Throwable { // (2)
--population;
super.finalize();
}
}
! #
# 3
0: Hello
1: Hello
2: Hello
0: Bye
1: Bye
2: Bye
3: Hello
4: Hello
2 blobs alive
System.gc() A %
System.runFinalization() "
2 % Runtime
2) A Runtime
# )45 2
Runtime.getRuntime() Runtime
! Runtime #
void gc()
!A *# %
System.gc()
void runFinalization()
!A "
2 % System.runFinalization()
long freeMemory()
! , 0 )
45% #
long totalMemory()
! , 0 )
45
# #
& :- MemoryCheck
Finalizers & :. RunTime ,/0
)
45 ,
:0
,
70% ,
9D0
,
990 #
,
990 2 A ,
9.0
6 A # %
A # + #
System.gc() ,
9.0
%
!"#
! #
# 3
6 3
#
" '
# %
% #
# # %
# " # %
'
2 # #
OutOfMemoryException #
Review Questions
< # delete
2 #
$ < % #
+ obj1 obj2 %
obj1
+ # # % #
arg1%
System.out.println(msg);
2 ,
90
2 ,
.0
2 ,
-0
$ 2 ,
@0
2 ,
80
% 2 ,
B0
+ # finalize()
%
2 finalize()
< finalize()
$ finalize() #
# finalize()
finalize()
& E
# finalize()
finalize() # protected
2 finalize() # #
$ finalize()
finalize()
'
" E
< #
finalize() #
2 #
$ + A B% A#
B
2 % %
8.2 Initializers
+ " " %
"
" % #
%# new
class ConstantInitializers {
int minAge = 12; // (1) Non-static
static double pensionPoints = 10.5; // (2) Static
// ...
" # " #
new + % ,90#
minAge " 12 ConstantInitializers #
new + " % , . @%
--0
6 " " #
" ,
.0# pensionPoints
" 10.5 # " 2 % " %
2 " 1
# this super "
# " % "
# % +
# % " ,
90 NO_OF_WEEKS
" ,
.0 5 #
class MoreInitializers {
int noOfDays = 7 * NO_OF_WEEKS; // (1) Non-static
static int NO_OF_WEEKS = 52; // (2) Static
// ...
}
new % "
#
) A "
% "
" ? 1 "
1 1 % #
#
1 1 3 "
% %#
# !
"
+ % " ,
.0 1 %
width " 1 1
width " ,
.0 1
# % width
,
@0 ,
.0 + %# # this
# ,
-0% # 0 width
class NonStaticInitializers {
int length = 10; // (1)
// double area = length * width; // (2) Not Ok. Illegal forward reference.
double area = length * this.width; // (3) Ok, but width has default value 0.
int width = 10; // (4)
# ,
80 height " ,
80
1 " ,80
(sqSide = (height = 20)) & NonStaticInitializers #
height 20
1 1 A " #
&! $ $ ($
class Hotel {
private int noOfRooms = 12; // (1)
private int maxNoOfGuests = initMaxGuests(); // (2) Bug
private int occupancyPerRoom = 2; // (3)
< 3
occupancyPerRoom: 0
maxNoOfGuests: 0
After object creation:
occupancyPerRoom: 2
maxNoOfGuests: 0
+ "
, 87% .D90 + #
" % "
"
& :8 "
" ,-0 createHotelPool() ,@0%#
TooManyHotelsException ,.0 + createHotelPool()
throws % #
try1catch % " ,-0%# %
*# % " #
%
' !
class RoomOccupancyTooHighException
extends RuntimeException {} // (1) Unchecked Exception
class TooManyHotelsException
extends Exception {} // (2) Checked Exception
class Hotel {
// Static Members
private static int noOfHotels = 12;
private static Hotel[] hotelPool = createHotelPool(); // (3)
} catch (TooManyHotelsException e) {
noOfHotels = 10;
System.out.println("No. of hotels adjusted to " +
noOfHotels);
}
return new Hotel[noOfHotels];
}
// Instance Members
private int noOfRooms = 215;
private int occupancyPerRoom = 5;
private int maxNoOfGuests = initMaxGuests(); // (5)
< 3
" # static #
# ,
-0
class StaticInitializers {
StaticInitializers % final
,
90 " matrix " ,.0
% #
,-0
+ % "
, @9D% 9@:0
> " 2
" + " return
%
" " 3 #
this super "
# % "
1 1 & :B
# "
" 2 # ,
@0%#
sf1 2 ,
990 %
% # = # 1 #
# % # ,.0
%,80
% ,
/0 " 2
# " + %
)* ! $ + $ %
class StaticForwardReferences {
static int sf1 = sf2 = 30; // (7) Static field. Assignment to sf2 allowed
static int sf2; // (8) Static field
int if1 = 5; // (9) Non-static field
< 3
sf1: 50
sf2: 30
& :/ # " ,
80 # ,
B0
" 2 #%
%
,* ! - # $
class BankrupcyException
extends RuntimeException {} // (1) Unchecked Exception
class TooManyHotelsException
extends Exception {} // (2) Checked Exception
class Hotel {
// Static Members
private static boolean bankrupt = true;
private static int noOfHotels = 11;
private static Hotel[] hotelPool;
< 3
at ExceptionInStaticInitBlocks.main(ExceptionInStaticInitBlocks.java:33)
Caused by: BankrupcyException
at Hotel.<clinit>(ExceptionInStaticInitBlocks.java:26)
class InstanceInitializers {
squares " ,
90% #
" ,.0 InstanceInitializers >
" 2
" % , "
0
2 " % " #
1 1 + & ::%
# ,
@0%# nsf1
,990 % % #
= # 1 # # % #
,
.0%,-0
%,80
% ,
/0
! ! $ + $ %
class NonStaticForwardReferences {
int nsf1 = nsf2 = 30; // (7) Non-static field. Assignment to nsf2 allowed
int nsf2; // (8) Non-static field
static int sf1 = 5; // (9) Static field
< 3
nsf1: 50
nsf2: 30
2 " " #
#
.! ! - # /
class Base {
protected int a;
protected int b;
void print() {
System.out.println("a: " + a);
}
}
class AnonymousClassMaker {
Base createAnonymous() {
return new Base() { // (1) Anonymous class
{ // (2) Instance initializer
a = 5; b = 10;
}
void print() {
super.print();
System.out.println("b: " + b);
}
}; // end anonymous class
}
}
< 3
a: 5
b: 10
0 1 $ ! ! - #
class RoomOccupancyTooHighException
extends Exception {} // (1) Checked exception
class BankrupcyException
extends RuntimeException {} // (2) Unchecked exception
class Hotel {
// Instance Members
private boolean bankrupt = true;
private int noOfRooms = 215;
private int occupancyPerRoom = 5;
private int maxNoOfGuests;
< 3
at Hotel.<init>(ExceptionsInInstBlocks.java:26)
at ExceptionsInInstBlocks.main(ExceptionsInInstBlocks.java:32)
# %
; 3
+ 6
, B-% .@-0
+ " "
"
> "
# value ,
80
value 1 value #
" ,
80 value #
" ,B0
% 1 ,
-0
2 *
class SuperclassA {
public SuperclassA() { // (1)
System.out.println("Constructor in SuperclassA");
}
}
class SubclassB extends SuperclassA {
SubclassB() { // (2)
this(3);
System.out.println("Default constructor in SubclassB");
}
SubclassB(int i) { // (3)
System.out.println("Non-default constructor in SubclassB");
value = i;
}
{ // (4)
System.out.println("Instance initializer block in SubclassB");
value = 2; // (5)
< 3
Constructor in SuperclassA
Instance initializer block in SubclassB
Instance initializer expression in SubclassB
Non-default constructor in SubclassB
Default constructor in SubclassB
value: 3
# # 1 %
& :9. # #
"
this
%
" # #
# superValue ,
90 SuperclassA
" # SubclassB ,:0 SuperclassA
,
.0 doValue() ,-0 2 #
SuperclassA ,@0% SubclassB ,
/0
doValue() SubclassB ,-0
SuperclassA doValue() ,
@0
# SubclassB 5 #
% G
SubclassB " %
doValue ,-0 SubclassB
doValue() ,/0 SubclassB
value , 80 " ( %
"
! $ 2 *
class SuperclassA {
protected int superValue; // (1)
SuperclassA() { // (2)
System.out.println("Constructor in SuperclassA");
this.doValue(); // (3)
}
void doValue() { // (4)
this.superValue = 911;
System.out.println("superValue: " + this.superValue);
}
}
< 3
6 "
2 " " + "
" "
"
+ " "
2 "
% "
Review Questions
) ' # %# "
E
$ static ;
static {;}
% static { count = 1; }
, # # E
public MyClass(int m) {
System.out.println(i + ", " + j + ", " + k + ", " + l + ", " + m);
}
{ j = 70; l = 20; } // Instance Initializer Block
static { i = 50; } // Static Initializer Block
}
# % "
# % k# " #
# # # 5, 70, 0, 20, 0 #
% # # # 5, 7, 0, 20, 0 #
' # %# "
# # # E
MyClass { gap += 5; }
$ { ; }
{ length = 4.2; }
. # # E
{ m = msg("2"); }
String m = msg("3");
# # # 1%2% 3#
# # # 2%3% 1#
$ # # # 3%1% 2#
# # # 1%3% 2#
0 # # E
{ m = msg("2"); }
static { m = msg("3"); }
# # # 1%2% 3#
# # # 2%3% 1#
$ # # # 3%1% 2#
# # # 1%3% 2#
#
// # E
class GeomInit {
// int width = 14; /* Line A */
{
// area = width * height; /* Line B */
}
int width = 37;
{
// height = 11; /* Line C */
}
int height, area;
// area = width * height; /* Line D */
{
// int width = 15; /* Line E */
area = 100;
}
};
# #
$ $
&
Chapter Summary
# # 3
% #
"
" " % %