Java Constructor. If you want to get knowledge and clear understanding about Constructor in Java with Example. Then its a best place for you, here i will teach you in detail about Java constructor with very easy to understand examples.
What is Java Constructor:
In Java language, a constructor is used to provide initial
value for the object of the class. Constructor is a method in Java without any
data type and return value, we can say that a constructor is a special type of
method which actually initialize objects. It is called automatically when the
object of that class is created and at that time it provides or set values of
the object in memory.
Constructor in Java has no return type.
The name of the constructor should be exactly like class
name. For Example
If class name is Test, then constructor name will also be
Test.
For Example
public class Test
{
public
Test() //constructor
{
}
}
Whenever we create a class in Java, it will have a default
constructor in it, and we cannot set initial values for objects in default
constructor.
public class Test2
{
}
How Constructor Works
{
int a;
//variable a with no initialization
public
A() //constructor to initialize a variable
{
a
= 10;
}
public
static void main(String a[])
{
A
obj = new A(); //object created and constructor called
System.out.println(obj.a);//value
is printed
}
}
Output will be 10
Some Important Points about Java Constructor
· Name of constructor should
be like class name
·
Java constructor has no
data type
·
It will have no return type
·
It is used to initialize
Java class objects. It is used to provides value to objects
·
Constructor can be declared
as Public and Private
Default Constructor
If user doesn’t create his constructor in the class, in that
case, we have default constructor in the class
To explain default constructor in Java, we have to
understand a simple example.
Lets suppose, we have a class A and in this class, we have a
variable x of type int. When we make the object of this class, at that time, a
type of constructor is called and that constructor is developed by Java inside
your class, and this constructor is known as default constructor which will
provide a value to variable x and that value will be 0 because x is having
integer data type.
Program of Java Default Constructor
public class A
{
int x;
Public static
void main(String ar[])
{
A
obj = new A();
System.out.println(obj.x); //
Answer will be 0 because of default constructor and having type int
}
}
If we have another type of variable in a class, lets say y
having type double. In this case we will get answer as 0.0 because of default
constructor and having type double
Example Program
public class A
{
double y;
Public static
void main(String ar[])
{
A
obj = new A();
System.out.println(obj.y); //
Answer will be 0.0 because of default constructor and having type double
}
}
In case of String data type, we will get answer as null by
default constructor.
Example Program
public class A
{
String z;
Public static
void main(String ar[])
{
A
obj = new A();
System.out.println(obj.z); //
Answer will be null because of default constructor and having type String
}
}
No Argument Constructor in Java
In Java language, constructor is like a Java method and as
Java methods can accept arguments, So Java constructor can also accept
arguments. But whenever we have a constructor which does not accept any
argument, that constructor is known as Java No argument constructor. It can be
public or private
Example 1
Private No Argument Constructor
public class A
{
int a;
private A() //no argument constructor
{
a
= 10;
}
public
static void main(String ar[])
{
A
obj = new A();
System.out.println(obj.a);
// output is 10
}
}
As in above example, we have a private constructor which is
without any argument and just initializing the value of variable a to 10. This
constructor is known as no argument constructor. As we have only one class
here, so constructor can be private because it can be accessed with in the same
class
Example 2
Public No Argument Constructor
class A {
int a;
public A() {
a = 100;
}
}
public class B { // Another class
public static void main(String[]
args) {
// object is
created in another class
A obj = new A();
//object creation
System.out.println(obj.a); //printing result
}
}
Output is 100
In above example, we have two classes, and our constructor
is in first class having public keyword. This is because, we are actually
making object in second class, so constructor should be public in order to
access the data from another class
Parameterized or Argument Constructor in Java
In Java, if constructor accepts argument or more than one
argument. Than it is known as Argument or Parameterized constructor.
Example Program
Class A
{
int x;
//class level variable
A(int
a) // Constructor accepting one argument
{
x = a;
//Assigning the value to variable x using Java parameterized constructor
System.out.println(x);
//Printing the value
}
Public static void main(String ar[])
{
A obj =
new A(10); //sending the value to constructor
}
}
Output will be 10
Example Program 2
Class A
{
String
name;
A(String
a) //constructor accepting String argument
{
name =
a; //assigning the value to class variable name
System.out.println(name);
// printing the value
}
Public static void main (String arg[])
{
A obj = new A(“Java”); //First object creation and sending
value Java to parameterized constructor
A obj2 = new A(“Python”); //Second object creation and
sending value Python to parameterized constructor
}
}
Example Program 3
In this example, Java constructor will accept two arguments
Class A
{
int x,y;
A(int
a, int b)// Constructor accepting two values
{
x
= a; //assigning first value to variable x
y
= b; //assigning second value to variable y
System.out.println(x+y);
//adding and printing values
}
Public static void main(String ar[])
{
A obj = new A(10,10); //object creation and sending two
values to constructor
}
}
Output will be 20
Thank u sir
ReplyDelete