Scanner class Methods in Java

Java - Constructors

 Constructors

We have not discussed constructors, but we were already used them to create objects using new keyword.

Constructors are special methods.They are used to initialize the members of class.They have same name as class. A constructor can have public or no access modifier (default modifier).

Lets create an object of class, to remind how we previously used constructors.

       MyClass obj=new MyClass();

In the above example, we are creating object of class named "MyClass". Here, in the last we are calling a method with same name as class "MyClass();" .This is a constructor of "MyClass". We have not created any constructor yet but still we can call this one because its a Default constructor.It is generated by Compiler for us.

Definition: A constructor is a special method which is called whenever an object is created using new Keyword.It contains block of statements that can be used to initialize instance variables of an object before the reference of this object is returned by new keyword.In other words, we can pass some values in constructor which can initialize the values in the class before using them to calculate something.

A constructor may look like a method but it is different in two ways. First, the constructor does not have a return type,nor even void.It is because the constructor is automatically called by compiler.Second, it have same name as class. 

syntax:

constructor name (param1,param2...paramn)

{

//body of constructor

}

Here, constructor name must be same as class name.The parameters are optional.Constructor can have as many as parameters you want.

For example: This will demonstrate the use of constructor with no parameters to initialize the values used in a class.

Write a program to calculate the area of Rectangle using constructor and use it to initialize the variables of class.

Write a program to calculate the area of Rectangle using constructor and use it to initialize the variables of class.
Area of Rectangle using Constructor


In the above program,

  1. A class "Rectangle" is created and we declared two variables "length" and "breadth" in this class. These variables are declared in class so they can be accessed by any member within this class.
  2. Then a Constructor named "Ractangle()" is created.It is used to initialize the variables of class. This is very simple constructor.
  3. We created a method "area()" to calculate area.We can use length and breadth variables directly here.It returns the area after performing calculations.
  4. In the main method, We are creating a object of class Rectangle to call its method "area()".While creating an object we are calling constructor created by us,which assigns the values to variables.Then method is called using obj.The method took value of length and breadth from constructor and gave us the result on screen.
Output:

constructor example

Types of Constructors

There are three types of constructors:
  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor

1)Default Constructor: This constructor is generated by compiler. When we do not write constructor in program compiler generates a constructor by itself. It is an empty constructor.It does nothing.Constructors are always invoked  while creating objects.As I gave example above, this constructor is always available to use.

We need not to write a default constructor.Lets see how it looks like:

public MyClass(){}

It does not contain parameters and statements in the body.

Example: Lets see previous example, but this time without constructor to see working of default constructor.

Write a program to calculate the area of Rectangle using Default constructor.

Write a program to calculate the area of Rectangle using Default constructor.
Default Constructor

This program is same as previous, the only difference is we are not using any constructor to initialize variables "l" and "b".In the main method, we can still call constructor of class without creating it.This is Default construct which is always present to be used.

Output:

Write a program to calculate the area of Rectangle using Default constructor.

We will discuss about other types in later post.Stay tuned! Happy Coding!

Comments