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

JAVA CONSTRUCTOR

The document provides an overview of constructors in Java, detailing their purpose, characteristics, and examples of usage. It explains that constructors are special member functions invoked during object creation, can be overloaded, and must have the same name as the class. Several examples illustrate default and parameterized constructors, constructor overloading, and the use of the 'this' keyword.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

JAVA CONSTRUCTOR

The document provides an overview of constructors in Java, detailing their purpose, characteristics, and examples of usage. It explains that constructors are special member functions invoked during object creation, can be overloaded, and must have the same name as the class. Several examples illustrate default and parameterized constructors, constructor overloading, and the use of the 'this' keyword.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

St.

Thomas’ College of Engineering & Technology

JAVA
LECTURE : CONSTRUCTOR

A. K. SIROMONI
( AIML )
CONSTRUCTOR

• Is a member function in the class which is invoked automatically


when an object is created for that class.

• No return datatype is allowed in constructor function , however


it returns reference of the abject created by default.

• Constructors can be overloaded

• Constructors are used to provide the initial values of the data


members of the object during creation.

• Constructor function name should be same as class name

• All Object Oriented Languages supports Constructor

A.K.SIROMONI STCET CSE (AIML) 2


CONSTRUCTOR
EXAMPLE 1 - FILE NAME (myclass1.java)

// Declaring Constructor
import java.util.*;

public class myclass1 {


public myclass1(){System.out.println("myclass1");}
public static void main (String args[]){
// calling default constructor
myclass1 c = new myclass1();
}
}
OUTPUT : myclass1

A.K.SIROMONI STCET(AIML) 3
CONSTRUCTOR
EXAMPLE 2 - FILE NAME (myclass2.java)

// Calling parameterized constructor


import java.util.*;

public class myclass2 {


public myclass2(int i){System.out.println(i+" myclass2");}
public static void main (String args[]){
myclass2 c= new myclass2(5); }
}

OUTPUT : 5 myclass2

A.K.SIROMONI STCET(AIML) 4
CONSTRUCTOR
EXAMPLE 3 - FILE NAME (myclass3.java)

// Constructor Overloading , calling default constructor and calling parameterized


constructor
import java.util.*;
public class myclass3 {
// constructor overloading
public myclass3(){System.out.println(" default myclass3");}
public myclass3(int i){System.out.println(i+" myclass3");}

public static void main (String args[]){


myclass3 b= new myclass3(); // calling default Constructor
myclass3 c= new myclass3(5); // calling parameterized Constructor
} }

OUTPUT : default myclass3


5 myclass3

A.K.SIROMONI STCET(AIML) 5
CONSTRUCTOR
EXAMPLE 4 - FILE NAME (myclass4.java)

// Accessing members of unnamed object and present object with 'this'


import java.util.*;
public class myclass4 {
int val;

public myclass4(){val =4 ;}

public myclass4(int val){this.val=val;} // accessing member of present object


public static void main (String args[]){
System.out.println(new myclass4().val); // accessing member of unnamed object
System.out.println(new myclass4(10).val); // accessing member of present object
} }

OUTPUT : 4
10

A.K.SIROMONI STCET(AIML) 6
COPY CONSTRUCTOR
EXAMPLE 9 - FILE NAME (function8.java)

//COPY CONSTRUCTOR
// Compare result with output of Example 7
import java.io.*;
class one { int i;
String c;
one( one o ){ this.i = o.i; this.c = "b"+o.c;} // copy constructor
one(int i, String s){this.i = i; c=s; }
void incr(){ i++;}
void tell(){ System.out.println("object "+c+" is having value "+i);} }
public class function8{
public static void main(String [] args){ OUTPUT :
one a = new one(11,"a"); object a is having value 11
// calling copy constructor object ba is having value 11
one b = new one(a); object a is having value 11
one c = a; after change
a.tell(); b.tell(); a.incr(); object a is having value 12
System.out.println("after change"); object ba is having value 11
a.tell(); b.tell(); c.tell(); object a is having value 12
} }
A.K.SIROMONI STCET(AIML) 7
CONSTRUCTOR
EXAMPLE 5 - FILE NAME (myclass5.java)
// Declaring a member to be final and trying to change it
import java.util.*;
public class myclass5 {
public int i; public char c; public float f;public boolean b;
myclass5(int i, char c, float f, boolean b){
this.i = i ; this.c = c ; this.f = f; this.b = b;}

public static void main (String args[]){ try{


Scanner sc = new Scanner(System.in);
final float f=8.9f;
f=f+1; Wrong , we cannot change value of a final member .
int d = sc.nextInt(); So it will be an Error. Even if this program is using
boolean b= true; try catch and finally block , as it is an error, not
int x = 10; exception , no .class file ( byte code file ) will be
char c='x'; created and it will not be executed.
myclass5 a= new myclass5(x/d,c,f,b);
System.out.println(a.i+" "+a.c+" "+a.f+" "+a.b); }
catch (Exception e) {System.out.println("Exception"); }
finally{ System.out.println("from finally");} } }

A.K.SIROMONI STCET(AIML) 8
CONSTRUCTOR
EXAMPLE 5 - FILE NAME (myclass5.java)
// Correct as we are not changing final member
import java.util.*;
public class myclass5 {
public int i; public char c; public float f;public boolean b;
myclass5(int i, char c, float f, boolean b){
this.i = i ; this.c = c ; this.f = f; this.b = b;}

public static void main (String args[]){ try{


Scanner sc = new Scanner(System.in);
final float f=8.9f;
int d = sc.nextInt();
boolean b= true; int x = 10; char c='x';
myclass5 a= new myclass5(x/d,c,f,b);
System.out.println(a.i+" "+a.c+" "+a.f+" "+a.b); }
catch (Exception e) {System.out.println("Exception"); }}

A.K.SIROMONI STCET(AIML) 9
CONSTRUCTOR
EXAMPLE 6 - FILE NAME (myclass6.java)
// Using finally block for try and exception block
import java.util.*;

public class myclass6 {


public int i; public char c; public float f; public boolean b;
myclass6(int i, char c, float f, boolean b){
this.i = i ; this.c = c ; this.f = f; this.b = b; }

public static void main (String args[]){ try {


Scanner sc = new Scanner(System.in);
final float f=8.9f;
int d = sc.nextInt();
boolean b= true; int x = 10; char c='x';
myclass6 a= new myclass6(x/d,c,f,b);
System.out.println(a.i+" "+a.c+" "+a.f+" "+a.b); }
catch(Exception e){ System.out.println("exception called "+e);}
finally{ System.out.println("from finally block");}
}}

A.K.SIROMONI STCET(AIML) 10

You might also like