constructor and constructor myths
Constructor:-
In java programming language all the no static variables that are defined in the class are known as instance variable of the class and in java every thing is represented as an object so that when a user creates a class want to use the functions and properties of that class then that person should have to creates a object of that class. Now the constructor must be used to create the object of the class. The main functioning of the constructor is to provide the Reference ID of the constructor generated by JVM.
Some Basic Rules to declare the constructor:-
1. constructor name should be same as the class name.
2. constructor return type does not specified by the programmer.
Syntax with example:-
class ABC
{
ABC()
{
}
}
constructor type:-
1. default
2. parameterized
1. default:-
constructor with the empty parameter list is called a default constructor.
2. parameterized:-
constructor with the parameter is called parameterized constructor.
Note:- constructor does not have the programmer defined return type does not mean that constructor do not have return type "JVM returns the reference ID of the constructor through the constructor." that means constructor is a non-static method whose return type is specified by the JVM.
Because of we writing the code for creating the object of any class suppose we have a class A then the object creation statement of this class is some thing like-
A obj=new A();
in the statement we call constructor and place it in the right side of the assignment operator and in the programming all the statement that are at the right side of the assignment operator must return some value. so that's clear that constructor return some value that we store in the reference variable "obj" of A.
so that the statement that "a constructor does not have any return value is partially correct"
I explain the javap tool and the how to create the constructor and what happen if we don't place any constructor in the class in the video whose link is following-
https://youtu.be/mx__f1hkWfU
In java programming language all the no static variables that are defined in the class are known as instance variable of the class and in java every thing is represented as an object so that when a user creates a class want to use the functions and properties of that class then that person should have to creates a object of that class. Now the constructor must be used to create the object of the class. The main functioning of the constructor is to provide the Reference ID of the constructor generated by JVM.
Some Basic Rules to declare the constructor:-
1. constructor name should be same as the class name.
2. constructor return type does not specified by the programmer.
Syntax with example:-
class ABC
{
ABC()
{
}
}
constructor type:-
1. default
2. parameterized
1. default:-
constructor with the empty parameter list is called a default constructor.
2. parameterized:-
constructor with the parameter is called parameterized constructor.
Note:- constructor does not have the programmer defined return type does not mean that constructor do not have return type "JVM returns the reference ID of the constructor through the constructor." that means constructor is a non-static method whose return type is specified by the JVM.
Because of we writing the code for creating the object of any class suppose we have a class A then the object creation statement of this class is some thing like-
A obj=new A();
in the statement we call constructor and place it in the right side of the assignment operator and in the programming all the statement that are at the right side of the assignment operator must return some value. so that's clear that constructor return some value that we store in the reference variable "obj" of A.
so that the statement that "a constructor does not have any return value is partially correct"
I explain the javap tool and the how to create the constructor and what happen if we don't place any constructor in the class in the video whose link is following-
https://youtu.be/mx__f1hkWfU
Comments
Post a Comment