0% found this document useful (0 votes)
19 views

Java Constructors

The document discusses Java constructors. It defines a constructor as a special method that is invoked when an object is instantiated. Constructors have the same name as the class and do not specify a return type. The document provides examples of no-argument, default, and parameterized constructors. It explains constructor overloading and the rules for creating constructors in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Constructors

The document discusses Java constructors. It defines a constructor as a special method that is invoked when an object is instantiated. Constructors have the same name as the class and do not specify a return type. The document provides examples of no-argument, default, and parameterized constructors. It explains constructor overloading and the rules for creating constructors in Java.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIVERSITY OF ENGINEERING & MANAGEMENT, JAIPUR

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. }

● The output will be:

Constructor Called
Value of x = 5

● Here, the MyClass() constructor is called when obj object is instantiated. A


constructor may or may not accept arguments.
No Argument Constructor
• If a Java constructor does not accept any parameters, it is a no-arg
constructor.

accessModifier ClassName() {
// constructor body
}

• Example of no-arg constructor


1. class MyClass {
2. int i; • The output will be:
3. // constructor with no parameter
4. private MyClass(){ Object created and i = 5
5. i = 5;
6. System.out.println("Object created and i = " + i);
7. } • MyClass() constructor doesn’t
8. public static void main(String[] args) { accept any parameters.
9. MyClass obj = new MyClass();
10. }
11. }
Java Constructors
1. class Company {
2. String domainName;
3. // object is created in another class
4. public Company(){
5. domainName = "oracle.com";
6. }
7. }
8.
9. public class MainClass {
10.
11. public static void main(String[] args) {
12. Company companyObj = new Company();
13. System.out.println("Domain name = "+ companyObj.domainName);
14. }
15. }

The output will be:

Domain name = oracle.com


Default Constructor
• If we do not create constructors yourself, the Java compiler will
automatically create a no-argument constructor during run-time. This
constructor is known as default constructor. The default constructor
initializes any uninitialized instance variables.

Type Default Value

boolean false

byte 0

short 0

int 0

long 0L

char \u0000

float 0.0f

double 0.0d

object Reference null


Default Constructor
Example: Default Constructor
1. class MyClass {
2. int a;
3. boolean b;
4.
5. public static void main(String[] args) {
6. MyClass obj = new MyClass();
7.
8. System.out.println("a = " + obj.a);
9. System.out.println("b = " + obj.b);
10. }
11. }

● The output will be:

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

You might also like