public
class
InitialValues {
boolean
t
;
char
c
;
byte
b
;
short
s
;
int
i
;
long
l
;
float
f
;
double
d
;
void
print(String s) {
System.
out
.println(s);
}
void
printInitialValues() {
print(
"boolean "
+
t
);
print(
"char "
+
c
);
print(
"byte "
+
b
);
print(
"short "
+
s
);
print(
"int "
+
i
);
print(
"long "
+
l
);
print(
"float "
+
f
);
print(
"double "
+
d
);
}
public
static
void
main(String[] args) {
InitialValues iv =
new
InitialValues();
iv.printInitialValues();
}
}
|
class
Tag {
Tag(
int
marker) {
System.
out
.println(
"Tag("
+ marker +
")"
);
}
}
class
Card {
Tag
t1
=
new
Tag(1);
//
Ⅰ①
Card() {
System.
out
.println(
"Card()"
);
//
Ⅰ④
t3
=
new
Tag(33);
//
Ⅰ⑤
}
Tag
t2
=
new
Tag(2);
//
Ⅰ②
void
f() {
System.
out
.println(
"f()"
);
//
Ⅱ⑥
}
Tag
t3
=
new
Tag(3);
//
Ⅰ③
}
public
class
OrderOfInitialzation {
public
static
void
main(String[] args) {
Card t =
new
Card();
//
Ⅰ
t.f();
//
Ⅱ
}
}
|
class
Bowl {
Bowl(
int
marker) {
System.
out
.println(
"Bowl("
+ marker +
")"
);
}
void
f(
int
marker) {
System.
out
.println(
"f("
+ marker +
")"
);
}
}
class
Table {
static
Bowl
b1
=
new
Bowl(1);
//
Ⅰ①
Table() {
System.
out
.println(
"Table()"
);
//
Ⅰ③
b2
.f(1);
//
Ⅰ④
}
void
f2(
int
marker) {
System.
out
.println(
"f2("
+ marker +
")"
);
}
static
Bowl
b2
=
new
Bowl(2);
//
Ⅰ②
}
class
Cupboard {
Bowl
b3
=
new
Bowl(3);
//
Ⅱ③
,
Ⅳ①
,
Ⅵ①
static
Bowl
b4
=
new
Bowl(4);
//
Ⅱ①
Cupboard() {
System.
out
.println(
"Cupboard"
);
//
Ⅱ④
,
Ⅳ②
,
Ⅵ②
b4
.f(2);
//
Ⅱ⑤
,
Ⅳ③
,
Ⅵ③
}
void
f3(
int
marker) {
System.
out
.println(
"f3("
+ marker +
")"
);
}
static
Bowl
b5
=
new
Bowl(5);
//
Ⅱ②
}
public
class
StaticInitialization {
public
static
void
main(String[] args) {
System.
out
.println(
"Creating new Cupboard() in main"
);
//
Ⅲ
new
Cupboard();
//
Ⅳ
System.
out
.println(
"Creating new Cupboard() in main"
);
//
Ⅴ
new
Cupboard();
//
Ⅵ
t2
.f2(1);
//
Ⅶ
t3
.f3(1);
//
Ⅷ
}
static
Table
t2
=
new
Table();
//
Ⅰ
static
Cupboard
t3
=
new
Cupboard();
//
Ⅱ
}
|
class
Cup {
Cup(
int
marker) {
System.
out
.println(
"Cup("
+ marker +
")"
);
}
void
f(
int
marker) {
System.
out
.println(
"f("
+ marker +
")"
);
}
}
class
Cups {
static
Cup
c1
;
static
Cup
c2
;
static
{
c1
=
new
Cup(1);
c2
=
new
Cup(2);
}
Cups() {
System.
out
.println(
"Cups()"
);
}
}
public
class
ExpilicitStatic {
public
static
void
main(String[] args) {
System.
out
.println(
"Inside main()"
);
Cups.
c1
.f(99);
// (1)
}
// static Cups x=new Cups();//(2)
// static Cups y=new Cups();//(2)
}
|
class
Mug {
Mug(
int
marker) {
System.
out
.println(
"Mug("
+ marker +
")"
);
}
void
f(
int
marker) {
System.
out
.println(
"f("
+ marker +
")"
);
}
}
public
class
Mugs {
Mug
c1
;
Mug
c2
;
{
c1
=
new
Mug(1);
c2
=
new
Mug(2);
System.
out
.println(
"c1&c2 initialized"
);
}
Mugs() {
System.
out
.println(
"Mugs()"
);
}
public
static
void
main(String[] args) {
System.
out
.println(
"Inside main()"
);
new
Mugs();
System.
out
.println(
"===new Mugs again==="
);
new
Mugs();
}
}
|
int
[] a;
a =
new
int
[rand.nextInt(20)];
|
Integer[] a =
new
Integer[rand.nextInt(20)];
for
(
int
i = 0; i < a.
length
; i++) {
a[i] =
new
Integer(rand.nextInt(500));
}
|
class
Art {
Art() {
System.
out
.println(
"Art constructor"
);
}
}
class
Drawing
extends
Art {
Drawing() {
System.
out
.println(
"Drawing constructor"
);
}
}
public
class
Cartoon
extends
Drawing {
public
Cartoon() {
System.
out
.println(
"Cartoon constructor"
);
}
public
static
void
main(String[] args) {
new
Cartoon();
}
}
|
class
Game {
Game(
int
i) {
System.
out
.println(
"Game constructor"
);
}
}
class
BoardGame
extends
Game {
BoardGame(
int
i) {
super
(i);
System.
out
.println(
"BoardGame constructor"
);
}
}
public
class
Chess
extends
BoardGame {
Chess() {
super
(11);
System.
out
.println(
"Chess constructor"
);
}
public
static
void
main(String[] args) {
new
Chess();
}
}
|
abstract
class
Glyph {
abstract
void
draw();
Glyph() {
System.
out
.println(
"Glyph() before draw()"
);
draw();
System.
out
.println(
"Glyph() after draw()"
);
}
}
class
RoundGlyph
extends
Glyph {
private
int
radius
= 1;
RoundGlyph(
int
r) {
radius
= r;
System.
out
.println(
"RoundGlyph.RoundGlyph(),radius="
+
radius
);
}
void
draw() {
System.
out
.println(
"RoundGlyph.draw(),radius="
+
radius
);
}
}
public
class
PolyConstructors {
public
static
void
main(String[] args) {
new
RoundGlyph(5);
}
}
|
class
Tag {
Tag(
int
marker) {
System.
out
.println(
"Tag("
+ marker +
")"
);
}
}
class
Insect {
private
int
i
= 9;
protected
int
j
,
m
;
Insect() {
System.
out
.println(
"i = "
+
i
+
", j = "
+
j
);
j
= 39;
}
private
static
int
x1
= print(
"static Insect.x1 initialized"
);
static
int
print(String s) {
System.
out
.println(s);
return
47;
}
Tag
t1
=
new
Tag(1);
}
public
class
Beetle
extends
Insect {
private
int
k
= print(
"Beetle.k initialized"
);
public
Beetle() {
System.
out
.println(
"k = "
+
k
);
System.
out
.println(
"j = "
+
j
);
System.
out
.println(
"m = "
+
m
);
}
private
static
int
x2
= print(
"static Beetle.x2 initialized"
);
public
static
void
main(String[] args) {
System.
out
.println(
"Beetle constructor"
);
Beetle b =
new
Beetle();
}
Tag
t2
=
new
Tag(2);
}
|