Tuesday, 30 August 2016

Constructor

Constructor
Constructor is a java features, it can be used to constructor objects.
The role of the Constructor in Object creation is to provide initial value inside the object.
In java applications, constructors are executed exactly at the time and creating object, not before creating object and not after creating objects.
In java applications, we are able to utilize constructors to provide initialization for class level variables.
Constructor name must be same at the respective class name.
Constructors are not having return types.
Constructors are not allowing the access modifiers like static, final, abstractor,…….
Constructor are able to allow the access modifiers like public, protected, <default> and private.
Constructors are able to allow “throws” keyword to bypass exceptions from present constructor to caller.
Syntax:- 
[Access_MOdifier] Class_Name([Param_List]) [throws Exception_list]
{
  -----Instructions-----
}

Note 1:-
If we provide a constructer in a class without the same name of the class name then compiler will raise an error like “Invalid Method declaration; return type required” because compiler will treat the provided constructor as normal java method without return type.
Note 2:-
In java methods return type in mandatory
Note 3:-  
In java Applications, constructors are not allowing return types, if we provide return type to the constructors, then compiler will not raise any error and JVM will provide any output. Because, the provided constructor is converted as normal java method with the same class. If we access this constructor like normal java method then the provide constructor will executed like normal java method.
Ex code:-

class A
{
                void A()
                {
                                System.out.println("A-Con");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                                a.A();
                }
}

Output:-
A-Con
Note 4:-
In java application , constructors are not allowing the access modifiers like static, final, abstractor,……..if we provide constructors with the above specified access modifiers then compiler will raise an error like “Modifier XXX not allowed here”.
Ex code:-
class A
{
                static A()
                {
                                System.out.println("A-Con");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                }
}

Status:-  compiler error, modifiers is not existed .
Note 5:-
In java applications, constructor are able to allows the access modifiers like public, protected, <default> and private. If we declare any constructor as private then the provided constructor is available up to the respective class, it is not available to outside of the respective class. If we want to create object for the respective class. Then we have to access private constructor in the same class.
There are two types of the constructors in java .
1.       Default constructor
2.       User defined constructor


No comments:

Post a Comment