• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic question about constructors

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, so constructors are just fancy setter methods, right?

Here's my question... I need to create "all required public constructors" for this project. I have the following instance variables:




I guess I neeed to make the following constructors....


or do I need constructors for each individual instance variable AND for each combonation of orders the variables could go in?
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, constructor is totally different things from setter method. Constructor normally is used to initialize instance variable, but it is not a must, it can be use to perform any operation that you like which execute when you create a new object (i.e instantiation).

Firstly, what did you means by "all required public constructors" ? there is nothing so called "required" in Java for constructor. If you did not create one, compiler will add in a no-args constructor.

Another thing is constuctor have nothing to do with instance variable although it can be use to initialize instance variable, whether to have a non-arg constructor or/and numerous args constructor depends on what you want to achieve when carry out instantiation (i.e create new object).

From your code, proven that you did not compile and test it out before asking the question. In the code provided by you above, all the arguments without its type will cause compile error.


Here, provided you with an example which demonstrate the above statements:
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did compile and run before I asked. I'm just trying to figure out if the 2 constructors I made is enough to satisfy the requirements of the project spec.

My app is fully functional and working.

The specification specifically states "creates all required public constructors, including one that uses all setters to initialize all private attributes"

My constructors are as follows:



I'm just not sure if I need more constructors or not to satisfy the requirement of "all required public constructors"
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to disagree with Lee and say that instance variables have a lot to do with constructors. The constructor is what you should be using to initialize the instance variables of your class. Constructors can be called with no parameters as Lee demonstrated above to create blank objects. But in the real world Most objects you create are going to have values.

Example:
You need to create a rectangle class.




-Hunter

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I completly agree. Let me explain it in different way. If I have something to initialize and those are mandetory instance variables then we usually put those in constructors and do not keep any default (blank) construtor. This is a way we restrict the comsumer of that class to go ahead without required fields being initialized.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This comment is about the application, rather than about Java. You only need one constructor, the one with all three variables. There is no reason to create an invoice until all three are known. It is an invitation to error if any have to be filled in later. There are no reasonable default values for any of the fields. When you actually write it, there is no reason to use any setters.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the replies.

I agree I only NEED one constructor and any more would be silly. I also agree that if I have the proper constructor, I wouldn't need setter methods.

I have decided to leave the class with the 2 constructors, the blank one and the one with all three. My reasoning for this is the spec asks for constructorS (plural)... and I can't think of any other ways to make a reasonable constructor with the three instance variables given. I am also leaving the setters because the spec asks for them, and it seems it is requiring me to use the setter inside the constructor. Seems backwards to me.

Any feedback is welcome.
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:Thanks for all the replies.

I agree I only NEED one constructor and any more would be silly. I also agree that if I have the proper constructor, I wouldn't need setter methods.

I have decided to leave the class with the 2 constructors, the blank one and the one with all three. My reasoning for this is the spec asks for constructorS (plural)... and I can't think of any other ways to make a reasonable constructor with the three instance variables given. I am also leaving the setters because the spec asks for them, and it seems it is requiring me to use the setter inside the constructor. Seems backwards to me.



I would say that having a number of constructors that just call different combinations of setters makes these constructors a question of user convenience only, they're in no way required. A user could as well create an object using the default contructor (the blank one) and then call setters to his hearts desire.

Now if you want a class to have a default constructor only, then you don't need to supply one because it's supplied automatically by Java. But if you supply non-default constructors and you want the defaullt constructor to be available too, then you must supply the default constructor yourself, Java won't do it for you. So in that case an explicit default constructor has become required. If it's not supplied by you it won't be available.
 
Lee Kian Giap
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hunter McMillen wrote:I'm going to disagree with Lee and say that instance variables have a lot to do with constructors. The constructor is what you should be using to initialize the instance variables of your class.



First, I will say thanks for disagreement ! because this encourage discussion.
Yes, as I said "constructor is normally is used to initialize instance variable", may I know which statement you disagreed with ? since I didn't mention that constructor have little or nothing to do with instance variable, so I feel a bit confuse on your disagreement.

To provide a more precise explanation on the relationship between constructor and initialization of instance variable, I will say using constructor to initialize instance variable the way to have guarantee initialization. First, using args constructor, the object of this class is created base on value that provided at runtime, which value pass into the constructor instance variable initialization. Second, using non-args constructor, simply assign value (literal value, or method return value) to the instance variable inside constructor.

However, the constructor is still not just for you to do initialization, and as I said it is not a must for initialization purpose. To be more clear in explanation, constructor is for you to carry out operations that is needed when creating new object, these operations includes initialization of instance variable. Example for operations other than initialization when you go deeper into java programming, such as call a base class method which control the concurrency (but one must not call the method in its class or subclass because instantiation haven't complete).

Hunter McMillen wrote: Constructors can be called with no parameters as Lee demonstrated above to create blank objects. But in the real world Most objects you create are going to have values.



Now, don't forget that there is object that created from class to be use without changing its state. Thus, you simply assign the value at the point you define the instance variable in the class, and therefore, you will use a args/non-args constructor just to create a new object without initialization of instance variable. So, even as you mentioned that in real world Most objects you create are going to have values, this doesn't justify that the value have to be assigned in constructor.

So, please don't mess two concept into one.


Janeice DelVecchio wrote: I did compile and run before I asked. I'm just trying to figure out if the 2 constructors I made is enough to satisfy the requirements of the project spec.



Sorry for saying that you didn't compile and test it, well please post a complete code next time to avoid misunderstanding.


Janeice DelVecchio wrote: The specification specifically states "creates all required public constructors, including one that uses all setters to initialize all private attributes"



After you post the full sentence of the requirements (bold portion), it get very clear what the project really need ! Please do post full sentence next time, thanks !




I think the above code will help, cheers ! ~
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic