-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constructors.java
60 lines (57 loc) · 2.02 KB
/
Constructors.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
public class Constructors {
public static void main(String args[]){
Student s1 = new Student();
s1.name = "Eshita";
s1.rollno=36;
s1.password="abcd";
s1.marks[0]=80;
s1.marks[1]=100;
s1.marks[2]=90;
Student s2 = new Student("Eshita");
Student s3= new Student(36);
Student s4 = new Student(s1);
s4.password="xyz"; //we updated the password
s1.marks[2]= 100;
for(int i = 0; i<3; i++){ //printing marks'value in shallow copy constructor
System.out.println(s4.marks[i]);
}
}
//write static in front of the class Student to indicate that it is a static nested class,
//which means it does not require an instance of the outer class to be instantiated.
//This allows Student to be instantiated independently, without needing an instance of the enclosing class Constructors.
static class Student{
String name;
int rollno;
String password;
int marks[];
Student(){
System.out.println("Non parametrized Constructor called.");
marks = new int[3];
}
Student(String name){
this.name = name;
System.out.println("Name= "+name);
}
Student(int rollno){
this.rollno=rollno;
System.out.println("Roll number= "+rollno);
}
//Shallow copy constructor
Student(Student s1){
marks= new int[3]; // allows to store array because if not there it gives error "Cannot store to int array because "s1.marks" is null"
this.name = s1.name;
this.rollno=s1.rollno;
this.marks= s1.marks;
}
//deep copy contructor
// Student(Student s1){
// marks = new int[3];
// this.name = s1.name;
// this.rollno = s1.rollno;
// for(int i = 0; i<marks.length; i++){
// this.marks[i]= s1.marks[i];
// System.out.println("Marks= "+marks[i]);
// }
// }
}
}