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

    Default final String in Constructor

    Hello,

    Is it possible to use a constant in a constructor ?

    Java Code:
    private final String X = "x"; 
    
    public Y() {
      this(X);
    }


    Each time i get an error message :
    Cannot reference X before supertype constructor has been called

    Kind regards

  2. #2
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    396
    Rep Power
    6

    Default

    your code does not make any sense.
    Mak
    (Living @ Virtual World)

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Well, seemingly not.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Not like this you can't.

    Until the parent constructor has been called you can't use any attributes of the child since the object hasn't been built yet, so those attributes don't really exist.

    What is it you are trying to achieve? A default value to use if the empty constructor is called? If so then make X static.
    Last edited by Tolls; 12-15-2010 at 11:33 AM. Reason: mashing the keyboard

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

    Thumbs up [solved]

    Hi,

    A default value to use if the empty constructor is called? If so then make X static.
    adding static solved my problem.

    thx.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Well, it solves your problem if my assumption of what you were trying to do was correct.
    Otherwise it might simply be a hack.

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

    Default

    Hi Tolls,

    Your assumption was correct.

    Thanks a lot

    Dipke

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

    Default

    Quote Originally Posted by Dipke View Post
    Your assumption was correct.
    Objects are like onions; they are constructed inner layers (superclass) first, outer layers (subclass) last. The compiler helps you (halfway) with this: a constructor should either have a call to this( ... ) (another constructor from the current class) or to super( ... ) (a constructor of the superclass). If you don't put a call to a super( ... ) constructor and if you don't have a this( ... ) call the compiler puts a call to the no-arg constructor there: super(). If all your constructors call another constructor from the same class the compiler flags it as an error.

    Only when the superclass constructor has finished the member variables of the current (subclass) are initialized. Before that moment the compiler forbids you to refer to any of them.

    I wrote 'the compiler helps you halfway' because a constructor (in a superclass) can call methods that are overridable. If such a method is overridden the overriding method will be called (in a subclass). I consider that an anomaly of the language implementation but I do understand why it was done that way. (let a heated discussion commence! ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    Hi JosAh,

    Thanks for the explanation.
    That is then also the reason why a setter must be final when used in constructor ?

    Grz
    Dimi

Similar Threads

  1. Final variable-constructor method.
    By D4rkNrG in forum New To Java
    Replies: 9
    Last Post: 10-13-2010, 11:18 AM
  2. Replies: 3
    Last Post: 03-26-2010, 07:09 AM
  3. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  4. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  5. Constructor with final parameters
    By pm_4u in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 08:36 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
  •