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

What is connection class in java?

498


What carriage return means?

516


Can constructor be protected in java?

497


What is primitive data type in java?

528


Is void a data type in java?

516






what is abstract class in Java?

636


Compare Mutex and Semaphore in java.

549


What is the history of java?

531


How do you stop a thread in java?

581


Is there a way to increase the size of an array after its declaration?

587


What is static import in java?

521


When should a function throw an exception?

586


What is nextline method in java?

545


What is multi-catch block in java?

609


Can a static class have a constructor?

506