-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPassingObject.java
More file actions
50 lines (47 loc) · 1.1 KB
/
PassingObject.java
File metadata and controls
50 lines (47 loc) · 1.1 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
class Dept{
int deptId;
String deptName;
Dept(int deptId , String deptName){
this.deptId =deptId;
this.deptName = deptName;
}
}
class Print{
// Pass by Value
double print(int empId , String name, double salary){
System.out.println("Id "+empId);
System.out.println("Name "+name);
salary = salary + 1000;
System.out.println("Salary "+salary);
return salary;
}
// Pass By Ref
Dept print(Employee obj){
System.out.println("Id "+obj.id);
System.out.println("Name "+obj.name);
obj.salary = obj.salary + 1000;
System.out.println("Salary "+obj.salary);
Dept d = new Dept(1001,"IT");
return d;
}
}
class Employee{
int id;
String name;
double salary;
Employee(int id , String name , double salary){
this.id = id;
this.name = name;
this.salary = salary;
}
}
public class PassingObject {
public static void main(String[] args) {
Employee ram = new Employee(1001,"Ram",9999);
Print print = new Print();
//print.print(ram.id, ram.name, ram.salary);
Dept d1= print.print(ram);
System.out.println("Now Salary is "+ram.salary);
System.out.println(d1.deptId+" "+d1.deptName);
}
}