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

S

SaravanaKumar

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
 
P

Paul Lutus

SaravanaKumar said:
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?
 
T

Tor Iver Wilhelmsen

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.
 
J

John C. Bollinger

SaravanaKumar said:
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
(e-mail address removed)
 
S

SaravanaKumar

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

class MyStaticClass {
private MyStaticClass()
{
}
}


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


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.
 
P

Paul Lutus

SaravanaKumar wrote:

/ ...
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.
 
T

Tony Morris

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top