943,521 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 12230
  • Java RSS
Oct 19th, 2007
0

Use of constructors

Expand Post »
what is the use of constructors in java when we can intialize objects directly
Similar Threads
Reputation Points: 37
Solved Threads: 0
Newbie Poster
tinamary is offline Offline
16 posts
since May 2007
Oct 19th, 2007
0

Re: Use of constructors

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,756 posts
since May 2007
Oct 19th, 2007
0

Re: Use of constructors

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?
Last edited by peter_budo; Oct 19th, 2007 at 12:08 pm.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004
Aug 9th, 2010
0
Re: Use of constructors
can you tell me why we create constructor?
Last edited by Pavie; Aug 9th, 2010 at 5:19 am. Reason: found some mistake
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Pavie is offline Offline
1 posts
since Aug 2010
Aug 9th, 2010
0
Re: Use of constructors
Click to Expand / Collapse  Quote originally posted by Pavie ...
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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Aug 9th, 2010
0
Re: Use of constructors
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.


Click to Expand / Collapse  Quote originally posted by javaAddict ...
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.
Reputation Points: 10
Solved Threads: 17
Junior Poster
java_programmer is offline Offline
118 posts
since May 2006
Aug 9th, 2010
-1
Re: Use of constructors
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.
Reputation Points: 10
Solved Threads: 6
Junior Poster in Training
new_programmer is offline Offline
53 posts
since Jul 2010
Jan 31st, 2011
-1
Re: Use of constructors
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.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Lourdupinto is offline Offline
12 posts
since Jan 2011
Jan 31st, 2011
0
Re: Use of constructors
@Lourdupinto to latte with very weak explanation.

Thread closed before another time traveller try to hit
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,653 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Button not working
Next Thread in Java Forum Timeline: readLine() Method





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC