<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Manoj Yarramsetti</title>
    <description>The latest articles on Forem by Manoj Yarramsetti (@manojyarramsetti5).</description>
    <link>https://forem.com/manojyarramsetti5</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F126862%2Fcfce7eaf-0771-4266-8dba-e8e94ce38112.jpg</url>
      <title>Forem: Manoj Yarramsetti</title>
      <link>https://forem.com/manojyarramsetti5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/manojyarramsetti5"/>
    <language>en</language>
    <item>
      <title>A Brief View of Java Constructors</title>
      <dc:creator>Manoj Yarramsetti</dc:creator>
      <pubDate>Tue, 08 Jan 2019 06:28:49 +0000</pubDate>
      <link>https://forem.com/manojyarramsetti5/a-brief-view-of-java-constructors-3ah2</link>
      <guid>https://forem.com/manojyarramsetti5/a-brief-view-of-java-constructors-3ah2</guid>
      <description>

&lt;p&gt;Introduction&lt;/p&gt;

&lt;p&gt;Java is one of the emerging and on-demand technologies in the current scenario. Being the most secure and reliable and having huge features, it was become one among the on-demand ones.&lt;/p&gt;

&lt;p&gt;We all know that the objects play a crucial role in Java and are created using initial values provided in two ways. It is a tedious approach initializing variables of the object in this manner.One way is using dot operator to access the instance variable and them assigning values individually. The other way is using Constructors- the easiest and the most preferred way to assign values to variables in Java.&lt;/p&gt;

&lt;p&gt;Now, let’s discuss about the Constructors and their importance in assigning values to variables in Java.&lt;/p&gt;

&lt;p&gt;Constructor&lt;/p&gt;

&lt;p&gt;A constructor is a special process of the  structure or class into &lt;a href="https://tekslate.com/advanced-java-training"&gt;object-oriented programming&lt;/a&gt; that initializes to us an object of that type. and can be used to define the values of the members of an object, either user-defined values or to default. A constructor is an instance methods and variables it usually has the same name as of class, &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Rules of the constructor:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;a. Constructor name should be same as a Class name.&lt;br&gt;
b. Constructor can’t have any return type even Void.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Constructor is executed during the object creation.&lt;/li&gt;
&lt;li&gt;A constructor can have an n number of arguments.&lt;/li&gt;
&lt;li&gt;The variable which is declared inside the constructor is called local variables.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Constructor can be categorised into 2 types:&lt;/p&gt;

&lt;p&gt;a. Default Constructor.&lt;br&gt;
b. User define constructor.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;User define constructor: &lt;/p&gt;

&lt;p&gt;The constructor which is generated by user is called user defined&lt;br&gt;
constructor. It is also called as an explicit constructor.&lt;br&gt;
It can be categorised into:&lt;/p&gt;

&lt;p&gt;i) User define parameterised Constructor: If the &lt;a href="https://tekslate.com/reactjs-training/"&gt;constructor&lt;/a&gt; is containing any arguments or if the&lt;br&gt;
user is passing any value inside the constructor is called user define parameterised constructor.&lt;/p&gt;

&lt;p&gt;Ex:&lt;/p&gt;

&lt;p&gt;public class Demo {&lt;br&gt;
Demo(String s, int i) // Parameterized constructor&lt;br&gt;
{&lt;br&gt;
System.out.println("value of I is "+i+ "\nvalue of S is "+s);&lt;/p&gt;

&lt;p&gt;}&lt;br&gt;
void display()// Method&lt;br&gt;
{&lt;br&gt;
System.out.println("I am inside the Display method");&lt;br&gt;
}&lt;br&gt;
public static void main(String[] args)&lt;br&gt;
{&lt;br&gt;
Demo d = new Demo("Rashda",10);&lt;br&gt;
d.display();&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Run as class your programme until your Output window displays the following:&lt;/p&gt;

&lt;p&gt;Output: &lt;/p&gt;

&lt;p&gt;value of I is 10&lt;br&gt;
Value of S is Rashda&lt;br&gt;
I am inside the Display method&lt;/p&gt;

&lt;p&gt;ii) User define Non parameterised Constructor: If the constructor is not having any argument or if&lt;br&gt;
the user is not passing any value inside the constructor is called user define non parameterised&lt;br&gt;
constructor.&lt;/p&gt;

&lt;p&gt;Ex:&lt;/p&gt;

&lt;p&gt;public class Demo {&lt;br&gt;
Demo() // Non parameterized constructor&lt;br&gt;
{&lt;br&gt;
System.out.println("I am inside the constructor");&lt;br&gt;
}&lt;br&gt;
void display()// Method&lt;br&gt;
{&lt;br&gt;
System.out.println("I am inside the Display method");&lt;br&gt;
}&lt;br&gt;
public static void main(String[] args) {&lt;br&gt;
Demo d = new Demo();&lt;br&gt;
d.display();&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;The programme then moves non parameterized  constructor on to the next line of code&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;I am inside the constructor&lt;br&gt;
I am inside the Display method&lt;/p&gt;

&lt;p&gt;Default Constructor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The constructor which is generated by compiler is called default Constructor.&lt;/li&gt;
&lt;li&gt;The Default Constructor does not have any implementation (statement).&lt;/li&gt;
&lt;li&gt;Every CLASS has a default constructor.&lt;/li&gt;
&lt;li&gt;Default constructor is also called as Implicit Constructor.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ex:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;public class Demo {&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;public static void main (String[] args) {&lt;br&gt;
Demo d = new Demo(10);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
output: gives compile time error , because there is no parameterized constructor, which is present&lt;br&gt;
inside the class.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;public class Sample{
public static void main(String[] args) {
Sample s = new Sample();
}
}&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now it becomes to the gives compile time error, because there is no parameterized constructor&lt;/p&gt;

&lt;p&gt;Output: &lt;/p&gt;

&lt;p&gt;No compile time error&lt;br&gt;
In the above program JVM executed the default constructor which is generated by compiler.&lt;/p&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If a user is defining any constructor inside the class, then compiler will not generate any default
constructor.&lt;/li&gt;
&lt;li&gt;If the user is not defining any constructor inside the class then only compiler will generate default
constructor.&lt;/li&gt;
&lt;li&gt;Constructor constructs an object and initialises the state.
Ex: public class Sample{
Sample(int i)
{
system.out.println(i);
}
public static void main(String[] args) {
Sample s = new Sample(10);
Sample s1 = new Sample();
}
}&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Output: &lt;br&gt;
gives compile time error, because there is no default constructor is present in the above&lt;br&gt;
class.&lt;/p&gt;

&lt;p&gt;Purpose of Constructor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object creation.&lt;/li&gt;
&lt;li&gt;To initialise the states i.e initialising the local variable values to the instance variables.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can’t access the local variable value outside the method and outside the constructor.&lt;/li&gt;
&lt;li&gt;Local variable value is within the method scope or within the constructor scope.&lt;/li&gt;
&lt;li&gt;Constructor can’t return any value.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ex:&lt;/p&gt;

&lt;p&gt;class Student&lt;br&gt;
{&lt;br&gt;
String name;&lt;/p&gt;

&lt;p&gt;int id;&lt;br&gt;
String collegename;&lt;br&gt;
Student(String n,int i,String c) // Constructor&lt;br&gt;
{&lt;br&gt;
name = n;&lt;br&gt;
id = i;&lt;br&gt;
collegename = c;&lt;br&gt;
}&lt;br&gt;
void display()&lt;br&gt;
{System.out.println("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
public class Sample {&lt;br&gt;
public static void main(String[] args) {&lt;br&gt;
Student S1 = new Student ("John",1,"KIT");&lt;br&gt;
S1.display();&lt;br&gt;
Student S2 = new Student ("Mike",2,"KIT");&lt;br&gt;
S2.display();&lt;br&gt;
Student S3 = new Student ("Adam",3,"KIT");&lt;br&gt;
S3.display();&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Input&lt;br&gt;
Print " Names"  "ID's"  "College Names"&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Name is John&lt;br&gt;
Id is 1&lt;br&gt;
college name is KIT&lt;br&gt;
Name is Mike&lt;br&gt;
Id is 2&lt;br&gt;
college name is KIT&lt;br&gt;
Name is Adam&lt;br&gt;
Id is 3&lt;br&gt;
college name is KIT&lt;/p&gt;

&lt;p&gt;Note:&lt;/p&gt;

&lt;p&gt;Whenever the local variable and instance variable name are same JVM will get&lt;br&gt;
confused to identify local variable and instance variable. If we want to overcome this&lt;br&gt;
problem we have to use this keyword. this is a keyword it always refers to the instance&lt;br&gt;
variables of the current class.&lt;/p&gt;

&lt;p&gt;Ex:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;class Student
{
String name;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;int id;&lt;br&gt;
String collegename;&lt;br&gt;
Student(String name, int id, String collegename) // Constructor&lt;br&gt;
{&lt;br&gt;
name = name;&lt;br&gt;
id = id;&lt;br&gt;
collegename = collegename;&lt;br&gt;
}&lt;br&gt;
void display()&lt;br&gt;
{&lt;br&gt;
System.out.println("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
public class Sample {&lt;br&gt;
public static void main(String[] args) {&lt;br&gt;
Student S1 = new Student ("John",1,"KIT");&lt;br&gt;
S1.display();&lt;br&gt;
Student S2 = new Student ("Mike",2,"KIT");&lt;br&gt;
S2.display();&lt;br&gt;
Student S3 = new Student ("Adam",3,"KIT");&lt;br&gt;
S3.display();&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Input:&lt;br&gt;
Print “name = name;&lt;br&gt;
       id = id;&lt;br&gt;
      collegename = collegename;”&lt;/p&gt;

&lt;p&gt;output:&lt;br&gt;
Name is Null&lt;br&gt;
Id is 0&lt;br&gt;
college name is Null&lt;br&gt;
Name is Null&lt;br&gt;
Id is 0&lt;br&gt;
college name is Null&lt;br&gt;
Name is Null&lt;br&gt;
Id is 0&lt;br&gt;
college name is Null&lt;/p&gt;

&lt;p&gt;Ex2:&lt;br&gt;
class Student&lt;br&gt;
{&lt;br&gt;
String name;&lt;br&gt;
int id;&lt;br&gt;
String collegename;&lt;br&gt;
Student(String name,int id,String collegename) // Constructor&lt;br&gt;
{&lt;/p&gt;

&lt;p&gt;this.name = name;&lt;br&gt;
this.id = id;&lt;br&gt;
this.collegename = collegename;&lt;br&gt;
}&lt;br&gt;
void display()&lt;br&gt;
{&lt;br&gt;
System.out.println("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
public class Sample {&lt;br&gt;
public static void main(String[] args) {&lt;br&gt;
Student S1 = new Student ("John",1,"KIT");&lt;br&gt;
S1.display();&lt;br&gt;
Student S2 = new Student ("Mike",2,"KIT");&lt;br&gt;
S2.display();&lt;br&gt;
Student S3 = new Student ("Adam",3,"KIT");&lt;br&gt;
S3.display();&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Input:&lt;br&gt;
String name;&lt;br&gt;
int id;&lt;br&gt;
String collegename;&lt;/p&gt;

&lt;p&gt;Print "this.name = name;&lt;br&gt;
this.id = id;&lt;br&gt;
this.collegename = collegename;"&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;p&gt;Name is John&lt;br&gt;
Id is 1&lt;br&gt;
college name is KIT&lt;br&gt;
Name is Mike&lt;br&gt;
Id is 2&lt;br&gt;
college name is KIT&lt;br&gt;
Name is Adam&lt;br&gt;
Id is 3&lt;br&gt;
college name is KIT&lt;/p&gt;

&lt;h1&gt;
  
  
  Default Value:
&lt;/h1&gt;

&lt;p&gt;String = Null&lt;br&gt;
int = 0&lt;br&gt;
byte = 0&lt;br&gt;
short = 0&lt;br&gt;
long = 0&lt;br&gt;
float = 0.0&lt;br&gt;
double = 0.0&lt;br&gt;
char = space&lt;br&gt;
Boolean = false&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;Thus, constructors add a lot on usability and readability of any class in Java. It would be difficult to test classes since object creation without the knowledge about the constructor and it’s working.  &lt;/p&gt;

&lt;p&gt;In this article, we’ve learned about how constructors work in Java and why they’re essential. In fact, using constructor has become easier as you can use dependency injection for class initialization. While working with open sources like Google Guice and Spring frameworks, it will become easy to use and test these classes with appropriate constructors.&lt;/p&gt;


</description>
      <category>java</category>
      <category>advancedjava</category>
      <category>constructors</category>
      <category>j2ee</category>
    </item>
  </channel>
</rss>
