Constructor in Java
Constructor in Java
com
http://www.javatpoint.com/constructor
Constructor in Java
next prev
Constructor in java is a special type of method that is used to initialize the object.
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.
1/6
Test it Now
Output:
Bike is created
int id;
3.
String name;
4.
5.
id = i;
6.
name = n;
7.
8.
9.
10.
11.
12.
s1.display();
13.
s2.display();
14.
15. }
Test it Now
Output:
111 Karan
222 Aryan
int id;
3.
String name;
4.
int age;
5.
6.
id = i;
7.
name = n;
8.
9.
10.
id = i;
11.
name = n;
3/6
12.
age=a;
13.
14.
15.
16.
17.
18.
s1.display();
19.
s2.display();
20.
21. }
Test it Now
Output:
111 Karan 0
222 Aryan 25
Java Method
int id;
3.
String name;
4.
4/6
5.
id = i;
6.
name = n;
7.
8.
Student6(Student6 s){
9.
id = s.id;
10.
name =s.name;
11.
12.
13.
14.
15.
16.
s1.display();
17.
s2.display();
18.
19. }
Test it Now
Output:
111 Karan
111 Karan
int id;
3.
String name;
4.
5.
id = i;
6.
name = n;
7.
8.
Student7(){}
9.
10.
11.
12.
13.
s2.id=s1.id;
14.
s2.name=s1.name;
15.
s1.display();
5/6
16.
s2.display();
17.
18. }
Test it Now
Output:
111 Karan
111 Karan
6/6