Results 1 to 3 of 3
  1. #1
    mcdhappy80 is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Question Overridable method call in the constructor - question

    I have a simple Java class with couple of private attributes in it.
    In the constructor of the class I'm setting the values of the attributes like this:
    Java Code:
    private string attr1;
    
    public ClassName(String value)
    {
         this.attr1 = value;
    }
    My question relates to using the setter method in the constructor for setting the attribute value.
    If the method is public I get the message from the title of this topic.
    When I change it to private everything is ok but do I need it then, since the private methods can be used from inside the class, or should I use the first example?
    To reformulate my question, should I use the class private setter method to set the class private attribute in the constructor or not?
    Does the performance suffer if I use it?
    To help You even more in answering, I will not have the need to set the attribute of the class outside the class.
    Thank You.

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

    Default Re: Overridable method call in the constructor - question

    The warning message has to do with the fact that "best practice" say you should not call overridable methods in your constructor. The problem is described at javapractices.com - with associated aspects of the problem at the links at the bottom of the page. Basically the object is not fully initialized when the overridden method is called. (I know, you aren't overriding anything, but if the setter is not private, it *might* be overridden by you or someone else...)

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overridable method call in the constructor - question

    Make the setter final, unless you expect a subclass to change how the setter functions (unikely).

    Indeed, unless you can envisage your class being subclassed, and that will be something that should be apparent from your design, then you may as well make the class 'final' instead.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 10:17 AM
  3. Is there anyway not to call superclass constructor ?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 10:01 PM
  4. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  5. constructor vs overridable method
    By pepe34 in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 11:26 PM

Tags for this Thread

Posting Permissions

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