good practice to initalize all instance variables with type String to emptry string??

Discussion in 'Java' started by Matt, Jun 23, 2004.

  1. Matt

    Matt Guest

    Is it a good practice to initalize all instance variables with
    type String to emptry string?

    #1 approach:
    public class A
    { private String name = "";
    private String address ="";
    //etc...
    }

    rather than

    #2 approach:
    public class A
    { private String name;
    private String address;
    //etc...
    }

    When I read Java books, the examples don't usually do #1 approach.
    The problem is if we don't initialize to empty string, when we call
    the getter method of that instance variable, it will return null.

    Please advise. Thanks!!
    Matt, Jun 23, 2004
    #1
    1. Advertising

  2. Matt

    Lee Weiner Guest

    In article <>, (Matt) wrote:
    >When I read Java books, the examples don't usually do #1 approach.
    >The problem is if we don't initialize to empty string, when we call
    >the getter method of that instance variable, it will return null.


    And...?

    If an instance variable hasn't been assigned a value, isn't null a good signal
    that it has never been set?

    Lee Weiner
    lee AT leeweiner DOT org
    Lee Weiner, Jun 23, 2004
    #2
    1. Advertising

  3. Matt

    Chris Smith Guest

    Matt wrote:
    > #1 approach:
    > public class A
    > { private String name = "";
    > private String address ="";
    > //etc...
    > }


    > When I read Java books, the examples don't usually do #1 approach.
    > The problem is if we don't initialize to empty string, when we call
    > the getter method of that instance variable, it will return null.


    Repeat ten times: "The compiler is my friend. The VM is my friend."

    You're considering a coding convention that appears designed to prevent
    errors, but not to produce correct code. That's a very bad combination.
    If there's one thing worse than broken code, it's broken code that
    silently screws up your life without a single error message.

    There's no way of avoiding it: a variable needs to be assigned a useful
    value before it's used. If you don't assign it a useful value, then you
    can't write useful code that uses that value. Garbage in, garbage out;
    code will be broken regardless of which specific useless value you
    assigned. It's better for broken code to get back a null than an empty
    String, because using the null reference will cause an error message and
    let you know that something is amiss, whereas the empty string is more
    likely to break something silently.

    Of course, that's if you admit the possibility of error. If you never
    make mistakes, then why bother typing ' = ""' at the end of your String
    declarations anyway.

    (And, of course, it goes without saying that if the empty String is a
    reasonable initial value, for example if the String will have things
    appended to it over time and needs to start out blank, then of course
    you should intialize it to an empty String; but specifying that
    initialization as any kind of a default is still a mistake.)

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Jun 23, 2004
    #3
  4. In article <>,
    (Matt) wrote:

    > Is it a good practice to initalize all instance variables with
    > type String to emptry string?
    >
    > #1 approach:
    > public class A
    > { private String name = "";
    > private String address ="";
    > //etc...
    > }
    >
    > rather than
    >
    > #2 approach:
    > public class A
    > { private String name;
    > private String address;
    > //etc...
    > }
    >
    > When I read Java books, the examples don't usually do #1 approach.
    > The problem is if we don't initialize to empty string, when we call
    > the getter method of that instance variable, it will return null.
    >
    > Please advise. Thanks!!


    #1 is perfectly good style if the getter is defined as returning an
    empty string when there's no set value. This technique is common for
    some GUI beans where the user should be presented with nothing rather
    than the word "null." In other cases there needs to be differentiation
    between null and an empty string, so you'd initialize the variables to
    null.

    And finally, member variables that MUST be set should be declared as
    final. You'll get a compiler error if you accidentally create a new
    constructor that does not set them.
    Kevin McMurtrie, Jun 23, 2004
    #4
  5. Matt

    Roedy Green Guest

    On Tue, 22 Jun 2004 22:51:01 -0700, Kevin McMurtrie
    <> wrote or quoted :

    > In other cases there needs to be differentiation
    >between null and an empty string, so you'd initialize the variables to
    >null.


    see http://mindprod.com/jgloss/void.html
    for a long discussion of the problem.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 23, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UE9uZnJp?=

    How to initalize COM from an ASP.NET

    =?Utf-8?B?UE9uZnJp?=, Mar 16, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    397
    Mark Rae
    Mar 18, 2005
  2. Jacek Dziedzic
    Replies:
    1
    Views:
    288
    Moonlit
    Nov 1, 2003
  3. vlsidesign
    Replies:
    26
    Views:
    950
    Keith Thompson
    Jan 2, 2007
  4. Replies:
    5
    Views:
    2,004
    Ben Pope
    Feb 14, 2006
  5. John
    Replies:
    7
    Views:
    322
    Bart Willems
    Mar 11, 2007
Loading...

Share This Page