• Post Reply Bookmark Topic Watch Topic
  • New Topic

why use a constructor and not just declare the values in the instance variables right from the begin  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

just wondered,

why use a constructor and not just declare the values in the instance variables right from the beginning?

is there a reason that is advantageous to use constructors?

thanks

 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a constructor is used to initialize a object with the given parameters. It allows you to construct an object of various flavors depending on the number of parameters passed to it. If no such concept existed, you would have to call the setters individually to customize your object and there could be bugs where your object is not initialized correctly if a setter was not called. Additionally, parameterized constructors make it compulsory for the caller to specify what are the needed parameters to construct a given object.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok salvin thanks

is there any limitations to what a constructor should contain...is it really just the building blocks of your object
or should it initialise every as[ect of your object?

or is your object designed completely with a constructor and then getters and setters manipulate the instance variables created by your constructor...

i have read a bit and watched a good few videos on constructors...i understand what they do and how they are used
though im not familiar in using them practically in a program say,i am creating...
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you shou‍ld give every class a constructor, even if it is never called as seen here in the Java® Language Specification. I believe you shou‍ld avoid creating more than one constructor if possible, and that every constructor shou‍ld initialise every instance field in the class. You will sometimes choose a different design, for example using factory methods instead of public constructors.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy Hunt's Rule #2: Always consider context

Standard consultant's answer: "It depends"

I like explicitly assigning values in the field declaration itself if their default values are different from the ones that Java already assigns them by default. This helps you avoid copy pasting code or if you prefer avoiding it, constructor chaining.
 
salvin francis
Bartender
Posts: 1649
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:
is there any limitations to what a constructor should contain...

A few limitations exist such as order of calling super() or this().

jon ninpoja wrote:
is it really just the building blocks of your object
or should it initialise every as[ect of your object?

Ideally you should initialize your object in such a way that it's not in an inconsistent state. Yes, its the building block for a given object.

jon ninpoja wrote:
or is your object designed completely with a constructor and then getters and setters manipulate the instance variables created by your constructor...

You cannot depend on getters and setters for everything. You may also initialize internal private field members in a constructor that you don't want to expose via mutators.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You cannot depend on getters and setters for everything. You may also initialize internal private field members in a constructor that you don't want to expose via mutators.


and how are you meant to know when you would want to do that?
 
Bernard Lewis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some classes need data in order to be able to initialize themselves, they can't always determine the data for themselves. Hence passing parameters via a constructor is a way to make that initial data available. Just consider a simple string class, it's really convenient to create a string with an initial value via it's constructor.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!