Any sense in using private static final for non-static classes?

Discussion in 'Java' started by Ricardo Palomares Martinez, Apr 28, 2007.

  1. Hi,

    I'm trying to find out possible memory leaks in a program. While they
    are not exactly the cause, I've found myself in front of some code
    like this:

    public class NonStaticClass {
    private static final String CONSTANT1 = "glossary_version";
    private static final String CONSTANT2 = "5.1";

    The class is not used in a static way, and usually there is none or
    one instances of it (it is used just while saving data of the
    program). I'm thinking that those "constants", of which there are a
    lot of them, shouldn't be static, as they are wasting space most part
    of the program execution. Am I right?

    TIA

    --
    If it's true that we are here to help others,
    then what exactly are the OTHERS here for?
    Ricardo Palomares Martinez, Apr 28, 2007
    #1
    1. Advertising

  2. Ricardo Palomares Martinez wrote:
    > I'm trying to find out possible memory leaks in a program. While they
    > are not exactly the cause, I've found myself in front of some code
    > like this:
    >
    > public class NonStaticClass {
    > private static final String CONSTANT1 = "glossary_version";
    > private static final String CONSTANT2 = "5.1";
    >
    > The class is not used in a static way, and usually there is none or
    > one instances of it (it is used just while saving data of the
    > program). I'm thinking that those "constants", of which there are a
    > lot of them, shouldn't be static, as they are wasting space most part
    > of the program execution. Am I right?


    It is very common to use static final for constants.

    If we are talking JSE and not JME then I would assume the
    memory used by constants is insignificant.

    Arne
    =?ISO-8859-15?Q?Arne_Vajh=F8j?=, Apr 28, 2007
    #2
    1. Advertising

  3. Ricardo Palomares Martinez

    david.karr Guest

    On Apr 28, 2:05 pm, Ricardo Palomares Martinez <>
    wrote:
    > Hi,
    >
    > I'm trying to find out possible memory leaks in a program. While they
    > are not exactly the cause, I've found myself in front of some code
    > like this:
    >
    > public class NonStaticClass {
    > private static final String CONSTANT1 = "glossary_version";
    > private static final String CONSTANT2 = "5.1";
    >
    > The class is not used in a static way, and usually there is none or
    > one instances of it (it is used just while saving data of the
    > program). I'm thinking that those "constants", of which there are a
    > lot of them, shouldn't be static, as they are wasting space most part
    > of the program execution. Am I right?


    You've gotten way off track if your goal is to investigate memory
    leaks. the space used by statics like this will be insignificant. If
    you want to make them use MORE space, then make them non-static (I'm
    not suggesting you do this). Defining private constants is a perfectly
    reasonably thing to do.
    david.karr, Apr 28, 2007
    #3
    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. Timo Nentwig
    Replies:
    31
    Views:
    1,183
    Chris Smith
    May 13, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,720
  3. Replies:
    5
    Views:
    501
    Chris Uppal
    Nov 17, 2006
  4. puzzlecracker
    Replies:
    2
    Views:
    458
    Daniel Pitts
    Jul 25, 2008
  5. Royan
    Replies:
    11
    Views:
    1,014
    Arne Vajhøj
    Oct 25, 2008
Loading...

Share This Page