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 difference between adapter class and listener?

501


Why java uses the concept of the string literal?

546


Is it possible to define a method in java class but provide it’s implementation in the code of another language like c?

581


Can we convert integer to string in java?

598


What methods are used in Servlet?Applet communication?

1667






How do you check if an arraylist is not empty?

579


How does the java compiler work?

529


How many types of parsers are there?

551


Why is String immutable?

624


What technique can be employed to compare two strings?

576


Give few difference between constructor and method?

522


What is collection api?

610


Why use string handling in Java?

583


What is Java Package and which package is imported by default?

597


What is matcher in java?

518