-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentMain.java
More file actions
48 lines (47 loc) · 1.88 KB
/
StudentMain.java
File metadata and controls
48 lines (47 loc) · 1.88 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
package BasicJavaPrograms.Objects;
class Student {
String name;
int age;
public void printInfo() {
System.out.println(this.name);
System.out.println(this.age);
}
// This is a non-parameterized (or default) constructor.
// It's called when an object is created without passing any arguments, e.g., Student s = new Student();
Student() {
}
// This is a parameterized constructor.
// It's used to create an object and initialize its fields with the provided values.
Student(String name, int age) {
this.name = name;
this.age = age;
}
// This is a copy constructor.
// It's used to create a new object by copying the values from an existing object of the same class.
Student(Student other) {
this.name = other.name;
this.age = other.age;
}
// Java does not have explicit deconstructors (like C++).
// Memory deallocation is handled automatically by the Java Virtual Machine's (JVM) garbage collector.
// Objects are garbage collected when they are no longer referenced.
}
public class StudentMain{
public static void main(String args[]){
// More efficient: Initialize directly using the parameterized constructor
Student student1 = new Student("Alice", 20);
// Using the copy constructor to create student3 from student1
Student student3 = new Student(student1);
student1.printInfo();
System.out.println();
// More efficient: Initialize directly using the parameterized constructor
Student student2 = new Student("Bob", 22);
student2.printInfo();
System.out.println();
Student s1=new Student("Atharv", 21);
s1.printInfo();
System.out.println("\n--- Using Copy Constructor ---");
System.out.println("Student 3 (copied from Student 1):");
student3.printInfo();
}
}