-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassingReference.java
71 lines (49 loc) · 1.41 KB
/
PassingReference.java
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
package com.OOP;
class BoxB{
int length;
int height;
int breadth;
static int boxCount; //execution starts when class is loaded...
BoxB(int l,int h,int b){
this.breadth = b;
this.height = h;
this.length = l;
boxCount++;
}
static {
System.out.println("from static block");
boxCount = 0;
}
boolean isEqual(BoxB b){ //passing object as a reference & call by reference
b.length++; //commemt for b1 == b2 true
if(length == b.length && breadth == b.breadth && height == b.height)
return true;
return false;
}
BoxB doubleBox() { //returning objects
BoxB temp = new BoxB(2*length,2*breadth,2*height);
return temp;
}
static void displayBoxCount() {
System.out.println("boxCount is: " + boxCount);
}
}
public class PassingReference {
static int increment(int a) { //call by value
return a++;
}
public static void main(String[] args) {
System.out.println("main starts here");
// int a = increment(10);
// System.out.println(a);
BoxB.displayBoxCount();
System.out.println("before creating the object");
BoxB b1 = new BoxB(2,2,2); //2,3,4 --> false
BoxB b2 = new BoxB(2,2,2);
System.out.println("after creating the object");
BoxB b3 = b1.doubleBox();
System.out.println(b3.length);
System.out.println(b1.isEqual(b2));
System.out.println(b2.length);
}
}