Instantiating a static class( Class with all static members - methods and variables)

Discussion in 'Java' started by SaravanaKumar, Oct 18, 2004.

  1. Hi is there any use in instantiating a class with all static members.
    For example Consider a class
    //start class dec
    public class MyConstants{
    public static final int CHECKING = 0;
    public static int SAVINGS = 1;

    public static final String getAString(){
    return "some SampleString";
    }
    }
    //end class
    Normally this class can be used like MyConstants.CHECKING,
    MyConstants.getAString etc.,

    But some of the clients instantiate this class like
    MyConstants mc = new MyConstants();
    and use the variables like mc.CHECKING, mc.getAString().,


    My Questions are
    1. By Instantiating the object will it improve the performance of the
    client classes? If so how?
    2. Is this a good code design to use a constant class like this?

    Thanks,
    SaravanaKumar
     
    SaravanaKumar, Oct 18, 2004
    #1
    1. Advertising

  2. SaravanaKumar

    Paul Lutus Guest

    SaravanaKumar wrote:

    > Hi is there any use in instantiating a class with all static members.
    > For example Consider a class
    > //start class dec
    > public class MyConstants{
    > public static final int CHECKING = 0;
    > public static int SAVINGS = 1;
    >
    > public static final String getAString(){
    > return "some SampleString";
    > }
    > }
    > //end class
    > Normally this class can be used like MyConstants.CHECKING,
    > MyConstants.getAString etc.,
    >
    > But some of the clients instantiate this class like
    > MyConstants mc = new MyConstants();


    You can, and may want to, prevent this by creating a special constructor:

    class MyStaticClass {
    private MyStaticClass()
    {
    }
    }

    > and use the variables like mc.CHECKING, mc.getAString().,
    >
    >
    > My Questions are
    > 1. By Instantiating the object will it improve the performance of the
    > client classes? If so how?


    No. This has no purpose and to some degree slows your program down.

    > 2. Is this a good code design to use a constant class like this?


    That depends. How are you using the class?

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 18, 2004
    #2
    1. Advertising

  3. (SaravanaKumar) writes:

    > 1. By Instantiating the object will it improve the performance of the
    > client classes? If so how?


    No, no improvement at all.

    > 2. Is this a good code design to use a constant class like this?


    Yes, until they added enums in 1.5; there you should probably use them
    instead.
     
    Tor Iver Wilhelmsen, Oct 18, 2004
    #3
  4. Re: Instantiating a static class( Class with all static members -methods and variables)

    SaravanaKumar wrote:

    > Hi is there any use in instantiating a class with all static members.
    > For example Consider a class
    > //start class dec
    > public class MyConstants{
    > public static final int CHECKING = 0;
    > public static int SAVINGS = 1;
    >
    > public static final String getAString(){
    > return "some SampleString";
    > }
    > }
    > //end class
    > Normally this class can be used like MyConstants.CHECKING,
    > MyConstants.getAString etc.,
    >
    > But some of the clients instantiate this class like
    > MyConstants mc = new MyConstants();
    > and use the variables like mc.CHECKING, mc.getAString().,
    >
    >
    > My Questions are
    > 1. By Instantiating the object will it improve the performance of the
    > client classes? If so how?


    No. It will have no noticeable effect on the performance of the method
    invocations.

    > 2. Is this a good code design to use a constant class like this?


    Absolutely not. Static class members should never be referenced through
    a variable -- it is an abomination that Java ever permitted it, but
    we're stuck with it now. The practice can be deceptive and confusing.

    Example 1:
    Your clients could just as well do this:

    void a Method() {
    MyConstants mc = null; // NOTE that mc is null
    System.out.println(mc.CHECKING + mc.getAString());
    }

    With no problem at all.

    Example 2:
    They could also change it to this without having any effect whatsoever
    on the observed behavior:

    class OurConstants extends MyConstants {
    // Not even the same type:
    public static final boolean CHECKING = true;
    public static final String getAString(){
    return "a different SampleString";
    }
    }

    void a Method() {
    MyConstants mc = new OurConstants();
    System.out.println(mc.CHECKING + mc.getAString());
    }


    On the other hand, it's not really your problem.


    John Bollinger
     
    John C. Bollinger, Oct 18, 2004
    #4
  5. Paul Lutus <> wrote in message news:<>...
    > SaravanaKumar wrote:
    >
    > > Hi is there any use in instantiating a class with all static members.
    > > For example Consider a class
    > > //start class dec
    > > public class MyConstants{
    > > public static final int CHECKING = 0;
    > > public static int SAVINGS = 1;
    > >
    > > public static final String getAString(){
    > > return "some SampleString";
    > > }
    > > }
    > > //end class
    > > Normally this class can be used like MyConstants.CHECKING,
    > > MyConstants.getAString etc.,
    > >
    > > But some of the clients instantiate this class like
    > > MyConstants mc = new MyConstants();

    >
    > You can, and may want to, prevent this by creating a special constructor:
    >
    > class MyStaticClass {
    > private MyStaticClass()
    > {
    > }
    > }
    >
    > > and use the variables like mc.CHECKING, mc.getAString().,
    > >
    > >
    > > My Questions are
    > > 1. By Instantiating the object will it improve the performance of the
    > > client classes? If so how?

    >
    > No. This has no purpose and to some degree slows your program down.
    >
    > > 2. Is this a good code design to use a constant class like this?

    >
    > That depends. How are you using the class?


    This class is used in the EJB's to reference the static variables
    present in the class.
     
    SaravanaKumar, Oct 19, 2004
    #5
  6. SaravanaKumar

    Paul Lutus Guest

    SaravanaKumar wrote:

    / ...

    >> > 2. Is this a good code design to use a constant class like this?

    >>
    >> That depends. How are you using the class?

    >
    > This class is used in the EJB's to reference the static variables
    > present in the class.


    Okay then, use final definitions or access methods. As pointed out by
    others, the present arrangement allows accessing classes to change the
    values.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 19, 2004
    #6
  7. SaravanaKumar

    Tony Morris Guest

    > > > Hi is there any use in instantiating a class with all static members.

    No - most code style checking tools will enforce that a class with all
    static public/protected APIs expose no public/protected constructors -
    CheckStyle for example.

    --
    Tony Morris
    http://xdweb.net/~dibblego/
     
    Tony Morris, Oct 19, 2004
    #7
    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. JFCM
    Replies:
    4
    Views:
    5,779
  2. Electric Co.
    Replies:
    3
    Views:
    435
    Electric Co.
    Aug 9, 2006
  3. Nita Rai
    Replies:
    1
    Views:
    464
    mlimber
    May 9, 2006
  4. Hicham Mouline
    Replies:
    5
    Views:
    2,455
    James Kanze
    Dec 19, 2008
  5. Replies:
    5
    Views:
    374
Loading...

Share This Page