Wednesday, October 5, 2016

Muhammad Akbar

Constructor in Java-Tutorial

Now that we have come to know the basics of method (there can be many more things related to methods, we will cover those step by step, don’t think we are done with methods), we want to get you introduced with another very important feature in java known as Constructor.
Remember the Book class we have used while understanding class and object concept.
Let’s do the same thing using Constructor.
Create a class as below:
package com.java;

public class BookWithConstrcutor 
{
	// instance variable
	String title;
	String author;
	String genre;
	double price;
	
	// constructor
	public BookWithConstrcutor(String title, String author, String genre, double price)
	{
		this.title = title;
		this.author = author;
		this.genre = genre;
		this.price = price;
	}
	
	// main method
	public static void main(String[] args) 
	{
		// creating first object of Book
		BookWithConstrcutor book1 = new BookWithConstrcutor("Java Complete Reference", "Herbert Schield", "Programming Language", 300.0);
		
		//creating second object of Book
		BookWithConstrcutor book2 = new BookWithConstrcutor("One, Two, Three, Infinity", "George Gamow", "Science", 600.0);
		
		System.out.println("I have two books. Which are" );
		System.out.println(book1.title + " written by: " + book1.author + " and the price is :" + book1.price);
		System.out.println("And");
		System.out.println(book2.title + " written by: " + book2.author + " and the price is :" + book2.price);
	}
}
As you can see the code is almost same as before except this part:
// constructor
public BookWithConstrcutor(String title, String author, String genre, double price)
{
	this.title = title;
	this.author = author;
	this.genre = genre;
	this.price = price;
}
Constructor is a special kind of method in java, it is use to create object, initialize instance variable. From now on, whenever you want to initialize instance variable while creating object, do it strictly in Constructor.
Constructor has the same name as the class.
Now look at this line of code:
BookWithConstrcutor book1 = new BookWithConstrcutor("Java Complete Reference", "Herbert Schield", "Programming Language", 300.0);
It is calling the Constructor with four values as expected by constructor. With which operator we are calling the constructor? The same old ‘new’. As before the line will create a new object of BookWithConstrcutor class and then assign the variable the values we are passing.
this.title = title;
this.author = author;
this.genre = genre;
this.price = price;
By these four line we are assigning values to the object properties. Here we have used another operator ‘this’. this.price will always refer to the instance variable ‘price’ as we have defined:double price;
this.price = price;
By this line we are saying that take the instance variable ‘price’ and for book1 reference assign the value 300.0 to it.
Same goes for other variables as well.
Also we have created the second object in the same manner, but with different values.
BookWithConstrcutor book2 = new BookWithConstrcutor("One, Two, Three, Infinity", "George Gamow", "Science", 600.0);
Now, think about it. In the Book class we had a code like:
Book book1 = new Book();
What we have called with the ‘new’ operator? Exactly, the constructor of Book class. But wait a minute, we have not defined any constructor, should it not give us a compilation error?
Here is another flavor of Java. Java provide a default constructor (a constructor without any parameter, just like Book()) when you are not explicitly defining any constructor. That constructor gets called when you write the code new Book().
But once you define any constructor, you are on your own. Like in the class
BookWithConstrcutor you cannot write a code now like below:
BookWithConstrcutor book = new BookWithConstrcutor(); // compilation error
In short, if you have created a constructor, you have to use it to create new object, you are no longer allowed to use the default constructor.
That’s it for today. We will have more discussion on method and constructor as overloading, overriding etc. But not before you get a chance to play around a little on today’s lesson.

Muhammad Akbar

About Muhammad Akbar -

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

Hello Every One thanks For Visit My site You Can Suggest any Idea About site And Related Education Information Provide me Its Best Content For you am Include in this Site And feel free contact And email now I hope This site very help full you