Repository navigation
Expand file tree
/
Copy pathClassDemo.java
More file actions
102 lines (72 loc) · 1.74 KB
/
Copy pathClassDemo.java
File metadata and controls
102 lines (72 loc) · 1.74 KB
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
101
102
/**
*
* static data and static methods
*
*/
class Box{
int length; //instance variables
int breadth;
int height;
static int boxCount; //when class is loaded
Box(int l,int b, int h){ //constructor
length = l;
breadth = b;
height = h;
boxCount++;
}
Box(){
length = -1;
breadth = -1;
height = -1;
boxCount++;
}
static {
System.out.println("from the static block");
boxCount = 0;
}
Box(int l){
length = breadth = height = l;
boxCount++;
}
Box(Box b){ //copy constructor
length = b.length;
breadth = b.breadth;
height = b.height;
boxCount++;
}
void setDim(int l,int b, int h){
length = l;
breadth = b;
height = h;
}
int volume(){
return length*breadth*height;
}
boolean isEqual(Box b) {
if(length==b.length && breadth == b.breadth && height == b.height)
return true;
return false;
}
static void displayBoxCount(){
System.out.println("Box count is " + boxCount);
}
}
public class ClassDemo {
public static void main(String[] args) {
System.out.println("main starts here....");
Box blackBox;
System.out.println("before creating box obj");
blackBox=new Box(5,4,3);
System.out.println("after creating box obj");
blackBox.setDim(6,4,3);
System.out.println("vol of black box is " + blackBox.volume()); //invoke
Box woodBox = new Box(30,24,25);
System.out.println("vol of wood box is " + woodBox.volume() );
woodBox.setDim(56,45,23);
Box b1 = new Box(6,5,3);
System.out.println("blackBox == b1 " + blackBox.isEqual(b1));
Box b2 = new Box(b1);
b2.length = 10;
Box.displayBoxCount();
}
}