0% found this document useful (0 votes)
19 views4 pages

Constructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views4 pages

Constructor

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Constructor : The main aim of constructor is to initialize object.

Constructor name is same as class name.


Every class has constructor.
Constructor doesnt conatin return type.
Constructor is called automatically when we create Object.
Every construtor has super keyword as first line if we write or not.

Note : Constructor is used to initialize an object .


new keyword is used to create object.

Example :

package constructor;

public class Student {

int rollno;
String name;

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student();


System.out.println(stu.toString());
}
}

Example :

package constructor;

public class Student {

int rollno;
String name;

public Student() {
rollno = 0;
name = null;
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student();


System.out.println(stu.toString());
}
}

Example :

package constructor;

public class Student {

int rollno;
String name;

public Student() {
rollno = 11;
name = "Santosh";
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student();


System.out.println(stu.toString());
}
}

Types of Constructor

1)default Constructor/0-parm Constructor /no arg Constructor


2)parameterized Constructor
3)private Constructor
4)Copy Constructor

1)default Constructor/0-parm Constructor /no arg Constructor:

package constructor;

public class Student {

int rollno;
String name;

public Student() {
rollno = 0;
name = null;
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student();


System.out.println(stu.toString());
}
}

2)parameterized Constructor

package constructor;

public class Student {

int rollno;
String name;

public Student(int rollno, String name) {


super();
this.rollno = rollno;
this.name = name;
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student(11, "Santosh");


System.out.println(stu.toString());
}
}

3) private Constructor

package constructor;

public class Student {

int rollno;
String name;

private Student() {
super();
// TODO Auto-generated constructor stub
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {


Student stu = new Student(); // we can create object here
System.out.println(stu.toString());
}
}

package constructor;

public class Test {

public static void main(String[] args) {

Student stu = new Student();// we cant create object here


System.out.println(stu.toString());
}

4)Copy Constructor

package constructor;

public class Student {

int rollno;
String name;

public Student() {
super();
rollno = 11;
name = "Santosh";
}

public Student(Student stu) {


super();
this.rollno = stu.rollno;
this.name = stu.name;
}

@Override
public String toString() {
return "Student [rollno=" + rollno + ", name=" + name + "]";
}

public static void main(String[] args) {

Student stu = new Student();


System.out.println(stu.toString());

Student stu1=new Student(stu);


System.out.println(stu1);
}
}

You might also like