Constructor
Syntax Java Constructors:-
The new
keyword creates
the object of class Myname and
invokes the constructor to initialize this newly created object.
Rules for creating java constructor
There
are basically two rules defined for the constructor.
-
Constructor name must be same as its class name
-
Constructor must have no explicit return type
Types of java constructors:-
-
Default constructor (no-arg constructor)
-
Parameterized constructor
Java Default Constructor:-
A
constructor is called "Default Constructor" when it doesn't
have any parameter.
Java compiler will automatically create a no-argument
constructor during run-time. This constructor is known as default
constructor. The default constructor initializes any uninitialized
instance variables.
Syntax of default constructor:-
<class_name>(){}
Default
Constructor Example:-
public class EX_Costructor {
public EX_Costructor() {
// null or non parameterized constructor;
}
public static void main(String[] args) {
EX_Costructor object = new EX_Costructor();
object.hello();
}
public void hello() {
System.out.println("Hello Constructor");
}
}
public class EX_Costructor {
public EX_Costructor() {
// null or non parameterized constructor;
}
public static void main(String[] args) {
EX_Costructor object = new EX_Costructor();
object.hello();
}
public void hello() {
System.out.println("Hello Constructor");
}
}
Output:-
Hello Constructor
Hello Constructor
Explanation: In the above class, you are not creating any constructor so compiler provides you a default constructor. Here 0 and null values are provided by default constructor because 0 value show integer and Null value show String.
Java parameterized constructor:-
A
constructor which has a specific number of parameters is called
parameterized constructor.
Parameterized
constructor is used to provide different values to the distinct
objects.
ClassName
(arg1,arg2, arg3,………argn){
//
constructor body
}
-
Constructor Can Take Value , Value is Called as – Argument.
-
Argument can be of any type i.e Integer, Character, Array or any Object.
-
Constructor can take any number of Argument
Constructor Overloading:-
Important notes:-
-
Constructors are invoked implicitly when you instantiate objects.
-
The two rules for creating a constructor are:
-
A Java constructor name must exactly match with the class name.
-
A Java constructor must not have a return type.
-
-
If a class doesn't have a constructor, Java compiler automatically creates a default constructor during run-time. The default constructor initialize instance variables with default values. For example: int variable will be initialized to 0
-
Constructor types:
-
No-Arg Constructor - a constructor that does not accept any arguments
-
Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined.
-
Parameterized constructor - used to specify specific values of variables in object
-
-
Constructors cannot be abstract or static or final.
-
Constructor can be overloaded but can not be overridden.
0 comments:
Post a Comment