Results 1 to 4 of 4
  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Setter in constructor

    Hello,

    When i write a class in BleuJ with setters in the constructor there is no error or warning.
    When i copy this into netbeans, there is a warning.
    I have to make the class or the setter final.
    What is the reason for that ?

    Java Code:
    public Name(String name) {
    this.name = name; }
    or

    Java Code:
    public Name(String name) {
    setName(name); }

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

    Default

    Quote Originally Posted by Dipke View Post
    Hello,

    When i write a class in BleuJ with setters in the constructor there is no error or warning.
    When i copy this into netbeans, there is a warning.
    I have to make the class or the setter final.
    What is the reason for that ?

    Java Code:
    public Name(String name) {
    this.name = name; }
    or

    Java Code:
    public Name(String name) {
    setName(name); }
    If you don't make that class final and don't make the setName( ... ) method private or final someone else is able to extend your class and overrid the setName( ... ) method. Your constructor (in your base class) will call that method in the extending class instead of your implementation. Nobody knows what that method can do; hence the warning ... As a rule of thumb: a constructor shouldn't call methods that can be overriden.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The method in the extending class will be called before the instance has had a chance to initialise itself. If the extending class's method depends on some initialisation your constructor will defeat that and possibly cause grief.

    Java Code:
    class Base {
        double value;
    
        Base(double value) {
            this.value = value;
            report();
        }
    
        void report() {
            System.out.println("Value is " + value);
        }
    }
    
    class Ext extends Base {
        int div = 1000;
    
        Ext(double value) {
            super(value);
        }
    
        void report() {
            // this looks innocent enough, but the beahviour
            // is a little counterintuitive
            System.out.println("Value in kilounits is " + (value / div));
        }
    }
    
    public class Test {
        public static void main(String args[]) {
            Ext test = new Ext(12345);
        }
    }
    Last edited by pbrockway2; 11-22-2010 at 07:57 AM.

  4. #4
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default

    Thanks for the clear explanation.
    It make sense.

    Kind regards

Similar Threads

  1. Replies: 4
    Last Post: 01-01-2009, 12:03 PM
  2. Replies: 0
    Last Post: 03-29-2008, 12:38 PM
  3. Replies: 0
    Last Post: 03-29-2008, 12:38 PM
  4. Replies: 0
    Last Post: 09-26-2007, 08:29 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:28 PM

Posting Permissions

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