Results 1 to 7 of 7
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Leaving a final field uninitialized

    Hi guys,

    I want to know the "best practices" for the following: I made a class that takes in a value for the cache, and I declared it as

    private final long REQUESTED_SIZE;

    Then, in my constructor, I have

    public Cache(size_of_cache){

    REQUESTED_SIZE = size_of_cache;

    }

    However, I know that leaving a variable uninitialized is usually considered sloppy code. So, I wanted to know how you guys would handle this.

    As always, any help is greatly appreciated.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,570
    Rep Power
    23

    Default Re: Leaving a final field uninitialized

    I think you did it the only way it could be done. The value for the initialization comes in the constructor call.

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

    Default Re: Leaving a final field uninitialized

    What would happen if you forgot to initialise REQUESTED_SIZE? (I'm guessing this is the justification for considering blank finals sloppy)

    -----

    [Edit] That wasn't expressed clearly enough. You can't forget; it isn't sloppy.

  4. #4
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Leaving a final field uninitialized

    Thanks for the quick reply Norm. So, this isn't considered sloppy? (I really wanted to declare the cache size with final, since altering the cache size while the program runs would result in a pretty quick crash!)

    Also, does anyone know why leaving a field uninitialized is considered sloppy? If I make sure to create the field before making any calls to it, where could the bugs appear?

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

    Default Re: Leaving a final field uninitialized

    You can initialise as part of the declaration, or in an initialiser block. Or in (all) constructors.

    And you must do one of these or it isn't compilable Java.

    -----

    How would the opponents of slop deal with a final whose initialisation could throw an exception?
    Last edited by pbrockway2; 12-15-2011 at 01:59 AM.

  6. #6
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Leaving a final field uninitialized

    Thanks Brock. At the moment, I wanted to allow the user to specify the size of the cache, so I couldn't initialize it before the constructor. Further, I can't declare and initialize in the constructor, since then I won't have access to it globally...

    I don't see any way around what Norm said.

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

    Default Re: Leaving a final field uninitialized

    Yes, I don't disagree with Norm. I was just pointing out there is another place to initialise the thing (an initialiser block - no good to the caller though). And that there is no good reason (that I can see) for viewing blank finals as sloppy.

Similar Threads

  1. Synchronization on non-final field
    By newbiejava in forum New To Java
    Replies: 20
    Last Post: 08-10-2010, 09:58 AM
  2. Replies: 12
    Last Post: 03-16-2010, 06:45 PM
  3. static final field
    By techie.it19 in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:12 AM
  4. FInal field cannot be assigned
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-13-2007, 02:26 PM
  5. Final field question
    By derrickD in forum Advanced Java
    Replies: 1
    Last Post: 04-28-2007, 10:37 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
  •