1,105,556 Community Members

Use of constructors

Member Avatar
tinamary
Newbie Poster
16 posts since May 2007
Reputation Points: 27 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

what is the use of constructors in java when we can intialize objects directly

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Can you elaborate a little more on the question? All object creation is done through constructors. If no constructor is specified, the compiler will supply a default empty constructor that basically does nothing. If you wish to provide info that initializes some values in the object when it's created, you provide a constructor for those parameters and initialize those values in that constructor.

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

Do you think that user of your application will be interested in reading your code and seting all variables by him self? Or do you think all variables are always initialized?

Member Avatar
Pavie
Newbie Poster
1 post since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

can you tell me why we create constructor?

Member Avatar
javaAddict
Nearly a Senior Poster
3,274 posts since Dec 2007
Reputation Points: 900 [?]
Q&As Helped to Solve: 448 [?]
Skill Endorsements: 8 [?]
Team Colleague
Featured
 
0
 

can you tell me why we create constructor?

We use/call constructors to create instances of objects. In that way we can call the methods of the class.

We define different constructors for different functionality based on their arguments.

Member Avatar
java_programmer
Junior Poster
130 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
-1
 

1. Constructor is use to create instance of a class. In case, you want to initialize your object at the time of creation, you have to define constructor. If you don't define any constructor, the default constructor defined by Object class is used to create the instance. Hence, without constructor, you can't create any object in java.

2. What do you mean by we can initialize directlt. I guess you want to say that we can set value directly. Initialize means setting the value at the time of object creation.

We use/call constructors to create instances of objects. In that way we can call the methods of the class.

We define different constructors for different functionality based on their arguments.

Member Avatar
new_programmer
Junior Poster in Training
53 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
 
-1
 

WHAT IS CONSTRUCTOR
--------------------
1. A constructor creates an Object of the class and by initializing all the instance variables and creating a place in memory to hold the Object.
2. It is called using "new "

Why we Need Constructor
-----------------------
1. For simple programs , initializing can be done by user. that will not be a prob.
But consider a case where there is 20 or more instance member.
If the programmer make the user to initialize all , that will be very bad way of programming.

Member Avatar
Lourdupinto
Newbie Poster
12 posts since Jan 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
-1
 

Actually we use constructor in java to have initialization done by the program it self when the time of objected is created.so when the object is initialized the the respected method will be alled or executed.

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 

@Lourdupinto to latte with very weak explanation.

Thread closed before another time traveller try to hit

Question Answered as of 1 Year Ago by peter_budo, java_programmer, Ezzaral and 4 others
Start New Discussion
Tags Related to this Article