Hello Guest EMAIL : This is test email by javabykiran Greeting_MessageHDFC.getEmailTxt(emailMsg) Greeting_Message greeting_MessageHDFC =new Greeting_Message ("Hi","KiranHDFC") Greeting_MessageBOA.getEmailTxt(emailMsg) Greeting_Message greeting_MessageBOA = new Greeting_Message ("Hello", "KiranBOA") Greeting_Message greeting_Message = new Greeting_Message() String smsMsg = "SMS: This is the best book for java learners" String emailMsg = "EMAIL :This is test email by javabykiran" It's for initialization of global variable.
CONSTRUCTOR IN JAVA CODE
What is the use of constructors? You can say that if some code needs to be executed at object creation, then we can write that code in constructor.
![constructor in java constructor in java](https://javagoal.com/wp-content/uploads/2020/04/2.png)
Mostly private constructors are used in singleton design pattern. Then you can say that the constructor must be private. If somebody says, if they want to create class, no one should instantiate it. You cannot write two constructors with the same number of parameters and same kind of types.Īny access specifiers can be applied to constructors, if private is applied to constructor, then we cannot create an object outside of class. you can write other constructors by changing the arguments. class.forName(“”).newInstance() // this point is covered in later chaptersĬonstructor can be overloaded i.e.this() // this point is covered in later chapters.super() // this point is covered in later chapters.
![constructor in java constructor in java](https://i.ytimg.com/vi/QW2P2qYL304/maxresdefault.jpg)
You can call the constructor with following ways: You cannot specify the return type for the constructor, but if you do specify, then it will be treated as a normal method. When you write any constructor, then default constructor will not be inserted by the JVM. When you are not writing any constructor inside the class then the default constructor will inserted by the JVM automatically. S1.Student() //calling the normal method,not the constructorĬonstructor will be used to initialize instance variable of the class with a different set of values, but it's not necessary to initialise it.