What is meant by constructor?

Answer Posted / dinesh

All the objects that are created must be given initial
values.There are two approach for this.
1)Using the dot operator to access the instance variables
and then assign values to them individually---This is a
tedious approach.
2)Use of getData method to initialize each object
individually.eg:add.getData(10,15)
To make this simpler Java supports a special type of method
called a Constructor,that enables an object to initialize
itself when it is created.Constructor have the same name as
the class itself and they do not specify a return type,not
even void,bcoz they return the instance of the class itself.

Is This Answer Correct ?    23 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why do we create public static method in java?

786


What does the @override annotation do?

739


A non-static inner class may have object instances that are associated with instances of the class’s outer class. A static inner class does not have any object instances.

754


When object is created and destroyed?

823


What are design patterns and please explain?

816


What classes of exceptions may be caught by a catch clause in java programming?

911


Why is singleton class used?

761


State one difference between a template class and class template.

828


What is the difference between equals() and == in java?

687


What is contract between hashcode and equal method?

784


What is bubble sorting in java?

795


What are abstract methods in java?

870


Can we overload the constructors?

744


Can we force garbage collector to run ?

771


What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. Import java.net.* Versus import java.net.socket)?

854