Java Constructors
Java Constructors
Course Name :
1
Java Constructors
by
Mr. Santanu Basak
Assistant Professor, Department of Computer Science & Engineering,
University of Engineering & Management, Jaipur
Java Constructors
● A constructor is similar to a method (but not actually a method) that is
invoked automatically when an object is instantiated.
● The Java compiler distinguishes between a method and a constructor by its
name and return type. In Java, a constructor has the same name as that of
the class, and doesn’t return any value.
class Test {
Test() {
// constructor body
}
}
● Here, Test() is a constructor; it has the same name as that of the class and
doesn’t have a return type.
Java Constructors
1. class Test {
2. void Test() {
3. // method body
4. }
5. }
• Here, Test() has the same name as that of the class. However, it has a return
type void. Hence, it’s a method not a constructor.
Java Constructors
1. class MyClass {
2. private int x;
3.
4. // constructor
5. private MyClass(){
6. System.out.println("Constructor Called");
7. x = 5;
8. }
9. public static void main(String[] args){
10. MyClass obj = new MyClass();
11. System.out.println("Value of x = " + obj.x);
12. }
13. }
Constructor Called
Value of x = 5
accessModifier ClassName() {
// constructor body
}
boolean false
byte 0
short 0
int 0
long 0L
char \u0000
float 0.0f
double 0.0d
a=0
b = false
Default Constructor
Example: Previous program can be written as:
1. class MyClass {
2. int a;
3. boolean b;
4.
5. public MyClass() {
6. a = 0;
7. b = false;
8. }
9. public static void main(String[] args) {
10.
11. MyClass obj = new MyClass();
12.
13. System.out.println("a = " + obj.a);
14. System.out.println("b = " + obj.b);
15. }
16. }
• The output will be:
a=0
b = false
Parameterized Constructor
• A constructor may also accept parameters. It's syntax is:
accessModifier ClassName(arg1, arg2, ..., argn) {
// constructor body
}
● The output will be:
● Example: Parameterized constructor
1. class Vehicle { 2 wheeler vehicle created.
2. int wheels; 3 wheeler vehicle created.
3. private Vehicle(int wheels){ 4 wheeler vehicle created.
4. wheels = wheels;
5. System.out.println(wheels + " wheeler vehicle created.");
6. }
7. public static void main(String[] args) {
8. Vehicle v1 = new Vehicle(2);
9. Vehicle v2 = new Vehicle(3); ● We have passed an argument of
10. Vehicle v3 = new Vehicle(4); type int (number of wheels) to the
11. } constructor during object
12. } instantiation.
Constructor Overloading
● If two or more constructors are different in parameters. For example:
1. class Company {
2. String domainName;
3. public Company(){
The output will be:
4. this.domainName = "default";
default
5. }
Constructor Overloading
6. public Company(String domainName){
7. this.domainName = domainName;
8. }
9. public void getName(){
10. System.out.println(this.domainName);
11. }
12. public static void main(String[] args) {
13. Company defaultObj = new Company();
14. Company programizObj = new Company("Constructor Overloading");
15. defaultObj.getName();
16. programizObj.getName();
17. }
18. }
Java Constructors
● Constructors are invoked implicitly when you instantiate objects.
● The two rules for creating a constructor are:
○ A Java constructor name must exactly match with the class name
(including case).
○ A Java constructor must not have a return type.
● If a class doesn't have a constructor, Java compiler automatically creates a
default constructor during run-time. The default constructor initializes
instance variables with default values. For example: int variable will be
initialized to 0
● Constructor types:
○ No-Arg Constructor: a constructor that does not accept any arguments
○ Default Constructor: a constructor that is automatically created by the
Java compiler if it is not explicitly defined.
○ Parameterized constructor: used to specify specific values of variables
in object
● Constructors cannot be abstract or static or final.
● Constructor can be overloaded but can not be overridden.
UNIVERSITY OF ENGINEERING & MANAGEMENT, JAIPUR
Reference/s
Java
The Complete Reference
by
Herbert Schildt
UNIVERSITY OF ENGINEERING & MANAGEMENT, JAIPUR
Thank
You