Constructor in Java
Constructor is a special type of method for create a instance of class. Constructor name is same as class name . It has no return type . There are two types of constructor in java . These are a) Default Constructor b) Parameterized
Constructor. Here is an example
Default Constructor :
public class ConstDemo
{
ConstDemo()
{
System.out.print("Default Constructor");
}
public static void main(String[] args)
{
ConstDemo demo = new ConstDemo();
}
}
Output : Default Constructor
Parameterized Constructor :
public class ConstDemo
{
ConstDemo(String str)
{
System.out.print(str);
}
public static void main(String[] args)
{
ConstDemo demo = new ConstDemo("Parameterized Constructor");
}
}
Output : Parameterized Constructor
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Constructor. Here is an example
Default Constructor :
public class ConstDemo
{
ConstDemo()
{
System.out.print("Default Constructor");
}
public static void main(String[] args)
{
ConstDemo demo = new ConstDemo();
}
}
Output : Default Constructor
Parameterized Constructor :
public class ConstDemo
{
ConstDemo(String str)
{
System.out.print(str);
}
public static void main(String[] args)
{
ConstDemo demo = new ConstDemo("Parameterized Constructor");
}
}
Output : Parameterized Constructor
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Comments
Post a Comment