Professional Documents
Culture Documents
Constructor
Constructor
Constructor
// A simple constructor.
class MyClass
{
int x;
//Following is the constructor MyClass()
{
x = 10;
}
}
call constructor to initialize objects as follows:
public class ConsDemo
{
public static void main(String args[])
{
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + " " + t2.x);
}
}
• Types of constructor:
• 1.Default constructor.
• 2.Parameterized constructor.
Default constructor:-
• A constructor that have no parameter is known as default
constructor.
• Default Constructor is also called as Empty Constructor which has
no arguments.
• Automatically called when we creates the object of class.
• Syntax of default constructor:
• <class_name>()
• { //constructor body }
• class Bike1
• {
• Bike1()
• {
• System.out.println("Bike is created");}
• public static void main(String args[])
• {
• Bike1 b=new Bike1();
• }
• }
• Output:
• Bike is created
Rule: If there is no constructor in a class, compiler automatically
creates a default constructor.
• purpose of default constructor :
• Default constructor provides the default values to the object
like 0, null etc. depending on the type.
• class Student{
• int id;
• String name;
• void display(){System.out.println(id+" "+name);}
• public static void main(String args[]){
• Student s1=new Student3();
• Student s2=new Student3();
• s1.display();
• s2.display(); } }
• Output:
• 0 null
• 0 null
Parameterized Constructor :-
• Constructor which has some Arguments.
• We have to create object of Class by passing some Arguments at
the time of creating object with the name of class.
class MyClass
{
int x;
MyClass() //Default
{ X=10;}
MyClass(int i ) // Parameterized
{
x = i;
}
}
Class Rectangle
{
int length;
int breath;
Rectangle()
{
length = 5;
breath = 6;
}
int area()
{
Int rectArea = length * breath;
Return rectArea;
}
}
Class Constructordemo
{
Public static void main(String args[])
{
Rectangle firstRect = new
Rectangle();
System.out.println(“Area of
Rectangle = ”+ firstrect.area());
}
}
ob.disp();
OverLoad ab=new OverLoad(4,5,6);
ab.show();
}
}
• class Student4{
• int id;
• String name;
•
• Student4(int i,String n){
• id = i;
• name = n;
• }
• void display(){System.out.println(id+" "+name);}
•
• public static void main(String args[]){
• Student4 s1 = new Student4(111,"Karan");
• Student4 s2 = new Student4(222,"Aryan");
• s1.display();
• s2.display();
• }
• }
• Test it NowOutput:
• 111 Karan 222 Aryan
Method Overloading:-
• Two methods with same name in one class with
different signature/parameter is called method overloading.
System.out.println(a + b);
}
public void add(String x,String y){
System.out.println(x + y);
}
public void add( char c, char d)
{
System.out.println(c + d);
}
c.add(7,8);
c.add("he", "llo");
c.add('n','k');
}
}
Output
15
hello
217
• overloaded methods:-
• Method overloading appear in the same class.
• Method having the same name but, have
different parameter lists.
• can have different return types.
• Static variable:-
• Syntax : <class-name>.<variable-name>
public class Svariables
{
private static int x = 0;
private int y = 3;
public SVariables()
{
x++;
y += 5;
System.out.println(y + " -- Welcome -- " + x);
}
public static void main(String args[])
{
SVariables s1 = new SVariables();
SVariables s1a = new SVariables();
SVariables s1b = new SVariables();
}
}
Output:-
8 -- Welcome – 1
8 -- Welcome -- 2
8 -- Welcome -- 3
• static method:-
• It is a method which belongs to the
class and not to the object(instance).
• A static method can access only static data. It can
not access non-static data (instance variables).
• A static method can call only other static
methods and can not call a non-static method
from it.
• A static method can be accessed directly by
the class name and doesn’t need any object.
• Syntax : <class-name>.<method-name>
• A static method cannot refer to “this” or “super”
keywords in anyway.