Constructors Using Some Basic Examples:

  • Constructors are the special methods whose name is the same as the class name.
  • Constructors do not have return types even void also.
  • Constructors will be invoked by the JVM automatically at the time of object creation.
  • Constructors are mainly used to initialize instance variables of class with a different set of values.
Examples:
Lab1.java
  1. class Lab1 {
  2. public static void main(String args[]) {
  3. Student stu1 = new Student();
  4. stu1.show();
  5. Student stu2 = new Student();
  6. stu2.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. void show() {
  13. System.out.println(sid + "\t" + sname);
  14. }
  15. }
Lab2.java
  1. class Lab2 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.sid = 99;
  5. stu.sname = "Ayush";
  6. stu.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. void show() {
  13. System.out.println(sid + "\t" + sname);
  14. }
  15. }
Lab3.java
  1. class Lab3 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.sid = 99;
  5. stu.sname = "Ayush";
  6. stu.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. Student() {
  13. System.out.println("Student Default Constructor");
  14. }
  15. void show() {
  16. System.out.println(sid + "\t" + sname);
  17. }
  18. }
Lab4.java
  1. class Lab4 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.sid = 99;
  5. stu.sname = "Ayush";
  6. stu.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. Student(int id, Stringsn) {
  13. System.out.println("Student 2-Arg Constructor");
  14. sid = id;
  15. sname = sn;
  16. }
  17. void show() {
  18. System.out.println(sid + "\t" + sname);
  19. }
  20. }
Lab5.java
  1. class Lab5 {
  2. public static void main(String args[]) {
  3. Student stu1 = new Student(88, "Ayush");
  4. stu1.show();
  5. Student stu2 = new Student(99, "Garg");
  6. stu2.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. Student(int id, Stringsn) {
  13. System.out.println("Student 2-Arg Constructor");
  14. sid = id;
  15. sname = sn;
  16. }
  17. void show() {
  18. System.out.println(sid + "\t" + sname);
  19. }
  20. }
Lab6.java
  1. class Lab6 {
  2. public static void main(String args[]) {
  3. Student stu1 = new Student();
  4. stu1.show();
  5. Student stu2 = new Student(99, "Garg");
  6. stu2.show();
  7. }
  8. }
  9. class Student {
  10. intsid;
  11. String sname;
  12. Student() {
  13. System.out.println("Student Default Constructor");
  14. }
  15. Student(int id, Stringsn) {
  16. System.out.println("Student 2-Arg Constructor");
  17. sid = id;
  18. sname = sn;
  19. }
  20. void show() {
  21. System.out.println(sid + "\t" + sname);
  22. }
  23. }
Lab7.java
  1. class Lab7 {
  2. public static void main(String args[]) {
  3. Student stu1 = new Student(99, "Ayu", "[email protected]", 9999);
  4. stu1.show();
  5. Student stu2 = new Student(88, "sh", "[email protected]");
  6. stu2.show();
  7. Student stu3 = new Student(77, "AG");
  8. stu3.show();
  9. Student stu4 = new Student();
  10. stu4.show();
  11. }
  12. }
  13. class Student {
  14. intsid;
  15. String sname;
  16. String email;
  17. long phone;
  18. Student(int id, Stringsn, Stringem, longph) {
  19. System.out.println("Student 4-Arg Constructor");
  20. sid = id;
  21. sname = sn;
  22. email = em;
  23. phone = ph;
  24. }
  25. Student(int id, Stringsn, Stringem) {
  26. System.out.println("Student 3-Arg Constructor");
  27. sid = id;
  28. email = em;
  29. sname = sn;
  30. }
  31. Student(int id, Stringsn) {
  32. System.out.println("Student 2-Arg Constructor");
  33. sid = id;
  34. sname = sn;
  35. }
  36. Student() {
  37. System.out.println("Student Default constructor");
  38. }
  39. void show() {
  40. System.out.println(sid + "\t" + sname + "\t" + "email" + "phone");
  41. }
  42. }
Lab8.java
  1. class Lab8 {
  2. public static void main(String args[]) {
  3. Student stu3 = new Student(77, "AG");
  4. stu3.show();
  5. }
  6. }
  7. class Student {
  8. intsid;
  9. String sname;
  10. Student(int id, Stringsn) {
  11. System.out.println("Student 2-Arg Constructor");
  12. sid = id;
  13. sname = sn;
  14. }
  15. Student(int x, int y) {
  16. System.out.println("Student 2-Arg constructor");
  17. sid = x;
  18. sname = y;
  19. }
  20. void show() {
  21. System.out.println(sid + "\t" + sname);
  22. }
  23. }
Lab9.java
  1. class Lab9 {
  2. public static void main(String args[]) {
  3. Student stu3 = new Student(77, "AG");
  4. stu3.show();
  5. }
  6. }
  7. class Student {
  8. intsid;
  9. String sname;
  10. Student(int id, Stringsn) {
  11. System.out.println("Student 2-Arg Constructor");
  12. sid = id;
  13. sname = sn;
  14. }
  15. Student(String y, int x) {
  16. System.out.println("Student 2-Arg constructor");
  17. sname = y;
  18. sid = x;
  19. }
  20. void show() {
  21. System.out.println(sid + "\t" + sname);
  22. }
  23. }
Lab10.java
  1. class Lab10 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.Student(77, "AG");
  5. stu.show();
  6. }
  7. }
  8. class Student {
  9. intsid;
  10. String sname;
  11. Student(int id, Stringsn) {
  12. System.out.println("Student 2-Arg Constructor");
  13. sid = id;
  14. sname = sn;
  15. }
  16. Student() {
  17. System.out.println("Student default constructor");
  18. }
  19. void show() {
  20. System.out.println(sid + "\t" + sname);
  21. }
  22. }
Lab11.java
  1. class Lab11 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.Student(77, "AG");
  5. stu.show();
  6. }
  7. }
  8. class Student {
  9. intsid;
  10. String sname;
  11. void Student(int id, Stringsn) {
  12. System.out.println("Student 2-Arg Constructor");
  13. sid = id;
  14. sname = sn;
  15. }
  16. Student() {
  17. System.out.println("Student default constructor");
  18. }
  19. void show() {
  20. System.out.println(sid + "\t" + sname);
  21. }
  22. }
Lab12.java
  1. class Lab12 {
  2. public static void main(String args[]) {
  3. Student stu = new Student();
  4. stu.Student(77, "AG");
  5. stu.show();
  6. }
  7. }
  8. class Student {
  9. intsid;
  10. String sname;
  11. void Student(int id, Stringsn) {
  12. System.out.println("Student 2-Arg Constructor");
  13. sid = id;
  14. sname = sn;
  15. }
  16. void show() {
  17. System.out.println(sid + "\t" + sname);
  18. }
  19. }
Lab13.java
  1. class Lab13 {
  2. public static void main(String args[]) {
  3. Student stu = new Student(-12);
  4. stu.show();
  5. }
  6. }
  7. class Student {
  8. int age = 18;
  9. Student(int ag) {
  10. System.out.println("Student 2-Arg Constructor");
  11. age = ag;
  12. }
  13. void show() {
  14. System.out.println(age);
  15. }
  16. }
Lab14.java
  1. class Lab14 {
  2. public static void main(String args[]) {
  3. Student stu = new Student(-12);
  4. stu.show();
  5. }
  6. }
  7. class Student {
  8. int age = 18;
  9. Student(int ag) {
  10. System.out.println("Student 2-Arg Constructor");
  11. if (ag = 18)
  12. return;
  13. age = ag;
  14. }
  15. void show() {
  16. System.out.println(age);
  17. }
  18. }
Lab15.java
  1. class Lab15 {
  2. public static void main(String args[]) {
  3. Student stu = new Student(-12);
  4. stu.show();
  5. }
  6. }
  7. class Student {
  8. int age = 18;
  9. Student(int ag) {
  10. System.out.println("Student 2-Arg Constructor");
  11. if (ag = 18)
  12. return 0;
  13. age = ag;
  14. }
  15. void show() {
  16. System.out.println(age);
  17. }
  18. }