| classbodyjavaOO variables |
Stack class's constructor
is Stack, the name
of the Rectangle class's constructor is Rectangle,
and the name of the
Thread class's constructor is Thread.
Stack defines a single
constructor:
public Stack() {
items = new Vector(10);
}
Java supports name overloading for constructors so that a class can
have any number of constructors, all of which have the same name.
Following is another constructor that could be defined by
Stack. This particular constructor sets the initial size
of the stack according to its parameter:
public Stack(int initialSize) {
items = new Vector(initialSize);
}
Both constructors share the same name, Stack, but they
have different parameter lists. The compiler differentiates these
constructors based on the number of parameters in the list and their
types.
Typically, a constructor uses its arguments to initialize the new object's state. When creating an object, choose the constructor whose arguments best reflect how you want to initialize the new object.
Based on the number and type of the arguments that you pass into the constructor, the compiler can determine which constructor to use. The compiler knows that when you write the following code, it should use the constructor that requires a single integer argument: new Stack(10); Similarly, when you write the following code, the compiler chooses the no-argument constructor or the constructor: new Stack(); When writing your own class, you don't have to provide constructors for it. The constructor is automatically provided by the runtime system for any class that contains no constructors. The provided by the runtime system doesn't do anything. So, if you want to perform some initialization, you will have to write some constructors for your class.
The constructor for the following subclass of Thread performs
animation, sets up some values, such as the frame speed and the
number of images, and then loads the images:
class AnimationThread extends Thread {
int framesPerSecond;
int numImages;
Image[] images;
AnimationThread(int fps, int num) {
super("AnimationThread);
this.framesPerSecond = fps;
this.numImages = num;
this.images = new Image[numImages];
for (int i = 0; i <= numImages; i++) {
. / Load all the images.
. . .
}
}
. . .
}
Note how the body of a constructor is like the body of a method; that
is, it contains local variable declarations, loops, and other
statements. However, one line in the AnimationThread
constructor that you wouldn't see in a method is the second line:
super("AnimationThread);
This line invokes a constructor provided by the superclass of
AnimationThread, namely, Thread.
This particular Thread constructor
takes a String that sets the name of Thread.
Often a constructor wants
to take advantage of initialization code written in a class's
superclass. Indeed, some classes must call their superclass constructor
in order for the object to work properly. If present, the superclass
constructor must be the first statement in the subclass's constructor:
An object should perform the higher-level initialization first.
You can specify what other objects can create instances of your class by using an access specifier in the constructors' declaration:
private
protected
public
| classbodyjavaOO variables |
Bhopal news
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100