Chapter 2 Java
Chapter 2 Java
S. A. Salunkhe
Properties of Constructor
A constructor must not have a return A method must have a return type.
type.
The Java compiler provides a default The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.
The constructor name must be same as The method name may or may not be
the class name. same as the class name.
• //Java program to overload constructors • void display()
• class Student5 • {
• { •
• int id; System.out.println(id+" "+name+" "+
• String name; age);
• int age; • }
• //creating two arg constructor
•
• Student5(int i,String n)
• public static void main(String args[]
• {
)
• id = i;
• {
• name = n;
• } Student5 s1 = new Student5(111,"Karan"
• //creating three arg constructor );
• Student5(int i,String n,int a) Student5 s2 = new Student5(222,"Aryan"
• { ,25);
• id = i; • s1.display();
• name = n; • s2.display();
• age=a; • }
• } • }
This Keyword
Using This Keyword Without This Keyword