Results 1 to 12 of 12
  1. #1
    BeLiPoWa is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Question Set methods in constructor?

    Hi, itīs a good practice to use set methods in the constructor of a class?

    Example:

    Java Code:
    public class Example {
    
    int number;
    
    public Example(int number){
    
    setNumber(number); // Instead of  this.number = number;
    
    }
    
    public void setNumber(int number) {
    
    this.number = number;
    
    }
    
    }

    Why? / Why not?

    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by BeLiPoWa View Post
    Hi, itīs a good practice to use set methods in the constructor of a class?

    Example:

    Java Code:
    public class Example {
    
    int number;
    
    public Example(int number){
    
    setNumber(number); // Instead of  this.number = number;
    
    }
    
    public void setNumber(int number) {
    
    this.number = number;
    
    }
    
    }

    Why? / Why not?

    Thanks.
    Suppose your class is extended and suppose that (non-private) setter method is overridden ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    snotmare is offline Member
    Join Date
    Apr 2011
    Posts
    34
    Rep Power
    0

    Default

    Greetings!

    I'm not sure if Jos would consider his scenario a case for or against using a setter, but I suppose it depends on what you're wanting your class to be used for. If you want to create a class that you intend to be extended and the behavior changed, always use the getter and setter methods and never reference the member (except within the getter and setter methods themselves).

    However, if you do not want extending classes changing the behavior and you are relying on your members being set in a specific way, then you'll want to reference them directly. Of course in this case, you probably wouldn't have a non-private getter and setter anyway, or if you do, you make defensive copies.

  4. #4
    BeLiPoWa is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Suppose your class is extended and suppose that (non-private) setter method is overridden ...

    kind regards,

    Jos
    Sorry, but I donīt understand, whatīs the problem in that case? :confused:

    Thanks.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Setters and getters really are used to provide other classes access to the classes instance variables. You don't need setters in a constructor since you are working inside of the class and you have direct access to the variables.

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    JosAH is for using getters and setters. What he meant was, what if a class extend your class and had specialized getters and setters? Using getters and setters in your constructor is recommended.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Perhaps I am wrong, or not understanding, however; I have never read or seen anything suggesting using setters inside of constructors. To me it seems like an unnecessary step. You have direct access to the variables inside the constructor, why not use it? You can also ensure that the variables are set correctly which keeps the instance variables safely encapsulated.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I wouldn't say that. You are adding extra unnecessary method calls to the Stack. Using a contrived example based upon what Jos said:
    Java Code:
    class Test {
        public static void main(String[] args) {
            Foo f = new Foo("Fred", "Flintstone");
            Bar b = new Bar("Barney", "Rubble");
            System.out.println(f);
            System.out.println(b);
        }
    }
    
    class Foo {
        String firstName;
        String lastName;
        
        Foo(String f, String l) {
            setFirstname(f);
            setLastname(l);
        }
        
        public void setFirstname(String f) {
            firstName = f;
        }
        
        public void setLastname(String l) {
            lastName = l;
        }
        
        public String toString() {
            return firstName + " " + lastName;
        }
    }
    
    class Bar extends Foo {
        Bar(String f, String l) {
            super(f, l);
        }
        
        public void setFirstname(String f) {
            lastName = f;
        }
        
        public void setLastname(String l) {
            firstName = l;
        }
    }
    It produces unexpected results.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    It does not produce unexpected results because you have set it up that way. If you need specialized getters and setters all you need to do is extend them, you would not have to worry about making your own constructor to call them instead of directly initializing them.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    As I said the example is contrived but the point is if the setter methods in the subclass need to do something different than the superclass then calling the setters in the constructor does not do what is expected rather than just setting the variables directly.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ra4king View Post
    It does not produce unexpected results because you have set it up that way. If you need specialized getters and setters all you need to do is extend them, you would not have to worry about making your own constructor to call them instead of directly initializing them.
    No, don't use overridable methods in your constuctor for a couple of reasons: Murphy, anything that can go wrong (you might not be the one who's extending your class) and even more important: when the super class (your class) is being constructed the sub class object isn't constructed yet so the overriding method runs in lala-land, i.e. it runs in an object that isn't even properly constructed yet.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @JosAH
    That is quite a good point. If a getter or setter needs to use another method in the class, then directly initializing the variables works better.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Help with constructor methods
    By mustachMan in forum New To Java
    Replies: 11
    Last Post: 12-16-2009, 02:28 AM
  3. Replies: 1
    Last Post: 07-20-2008, 04:42 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •