Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Constructor


Constructor function

What is Constructor

The constructor is special member function which initializes object when it is created.

The constructor is needed when we want to assign default values if user has not given.

Characteristics of Constructor

  • Constructor has same name as a class name
  • Constructor does not have any return type
  • Constructor does not return any value
  • Constructor can be overloaded as normal function
  • Constructor can accept any number of parameters
  • Constructor is called automatically when object is created
  • Constructor allocated the memory to object and garbage-collection deallocates the memory

Types of Constructors

There are two types of constructor

  • Default Constructor
  • Parameterized Constructor

Default Constructor

Default Constructor is called when object of class is created. When there is no constructor has been defined explicitly (by user), then java calls default constructor automatically.

We can define only one default constructor in the class. Java initializes all instance variables to zero after creation.

Default Constructor is also known as Zero Parameterized Constructor because it does not take any argument

Syntax

class classname
{
	classname ()	// default constructor
	{
		// body of constructor function
	}
}

Example of Default Constructor

Calculating area of Rectangle using Default constructor

class Rectangle
{
     int length,width;
     Rectangle()
     {
	 length = 12;
	 width = 8;
	 System.out.println("Values initialized");
     }
     int area()
     {
	 return  length * width;
     }
     public static void main(String args[])
     {
	 Rectangle rect = new Rectangle();
	 System.out.println("Area of Rectangle : "+rect.area());
     }
}
	

Output

Values initialized
Area of Rectangle : 96

In above example we have created class names as Rectangle which is having Default Constructor as Rectangle(). This constructor is executed when the object is created.


Parameterized Constructor

The Constructor which accepts the parameters known as Parameterized Constructor.

Sometimes it necesary to pass arguments to constructor while creating an object. It can have any number of parameters separated by commas.

Syntax

class className
{
   className( parameter1, parameter2, ... parameterN)
   {
	  body of Parameterized Constructor
	  ...
	  ...
   }
}

Example

Program to print the given string upto N times using Parameterized Constructor

class ParamDemo
{
  int number;
  String message;
  ParamDemo(String msg, int num)
  {
      message = msg;
      number = ntimes;
  }
	
  void print()
  {
      for (int i=1;i<=number;i++)
	System.out.println("msg : "+message);
  }
  public static void main(String args[])
  {
	ParamDemo pd = new ParamDemo("AndroidBerry", 7);
	pd.print();
  }
}

Output

msg : AndroidBerry
msg : AndroidBerry
msg : AndroidBerry
msg : AndroidBerry
msg : AndroidBerry
msg : AndroidBerry
msg : AndroidBerry

In above example we have printed the message AndroidBerry 7 times using Parameterized Constructor


<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo