Constructor in Java


SUBMITTED BY: rahulranjanmca

DATE: Jan. 5, 2016, 6:07 p.m.

FORMAT: Text only

SIZE: 3.6 kB

HITS: 32309

  1. Constructor in java is a special type of method that is used to initialize the object.
  2. Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
  3. Rules for creating java constructor
  4. There are basically two rules defined for the constructor.
  5. Constructor name must be same as its class name
  6. Constructor must have no explicit return type
  7. Types of java constructors
  8. There are two types of constructors:
  9. Default constructor (no-arg constructor)
  10. Parameterized constructor
  11. java constructor
  12. Java Default Constructor
  13. A constructor that have no parameter is known as default constructor.
  14. Syntax of default constructor:
  15. <class_name>(){}
  16. Example of default constructor
  17. In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.
  18. class Bike1{
  19. Bike1(){System.out.println("Bike is created");}
  20. public static void main(String args[]){
  21. Bike1 b=new Bike1();
  22. }
  23. }
  24. Test it Now
  25. Output:
  26. Bike is created
  27. Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
  28. default constructor
  29. Q) What is the purpose of default constructor?
  30. Default constructor provides the default values to the object like 0, null etc. depending on the type.
  31. Example of default constructor that displays the default values
  32. class Student3{
  33. int id;
  34. String name;
  35. void display(){System.out.println(id+" "+name);}
  36. public static void main(String args[]){
  37. Student3 s1=new Student3();
  38. Student3 s2=new Student3();
  39. s1.display();
  40. s2.display();
  41. }
  42. }
  43. Test it Now
  44. Output:
  45. 0 null
  46. 0 null
  47. Explanation:In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.
  48. Java parameterized constructor
  49. A constructor that have parameters is known as parameterized constructor.
  50. Why use parameterized constructor?
  51. Parameterized constructor is used to provide different values to the distinct objects.
  52. Example of parameterized constructor
  53. In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.
  54. class Student4{
  55. int id;
  56. String name;
  57. Student4(int i,String n){
  58. id = i;
  59. name = n;
  60. }
  61. void display(){System.out.println(id+" "+name);}
  62. public static void main(String args[]){
  63. Student4 s1 = new Student4(111,"Karan");
  64. Student4 s2 = new Student4(222,"Aryan");
  65. s1.display();
  66. s2.display();
  67. }
  68. }
  69. Test it Now
  70. Output:
  71. 111 Karan
  72. 222 Aryan
  73. Constructor Overloading in Java
  74. Constructor overloading is a technique in Java in which a class can have any number of constructors that differ in parameter lists.The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.
  75. Example of Constructor Overloading
  76. class Student5{
  77. int id;
  78. String name;
  79. int age;
  80. Student5(int i,String n){
  81. id = i;
  82. name = n;
  83. }
  84. Student5(int i,String n,int a){
  85. id = i;
  86. name = n;
  87. age=a;
  88. }
  89. void display(){System.out.println(id+" "+name+" "+age);}
  90. public static void main(String args[]){
  91. Student5 s1 = new Student5(111,"Karan");
  92. Student5 s2 = new Student5(222,"Aryan",25);
  93. s1.display();
  94. s2.display();
  95. }
  96. }

comments powered by Disqus