Showing posts with label new. Show all posts
Showing posts with label new. Show all posts

Tuesday, April 24, 2012

Constructors and instantiation

"Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/I67fRB )

Every class has at least one Constructor. This is a special method that prepares the object for use. It has the same name as the class.


A class can have multiple constructors - this is known as method overloading.

The constructor is always the first method called on an object. Think of it as an 'on-switch'. When we instantiate an object of a class, we call it's constructor using the new keyword:

Syntax:
Type identifier = new Type(parameters, if any);

Example:
Telephone myTelephone = new Telephone();


In UML notation, the constructor is always referred to as new(). This is because UML is language independent and not Java specific. Different programming languages have different rules for naming a constructor.