Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Instantiating a static class( Class with all static members - methods and variables) (http://www.velocityreviews.com/forums/t137728-instantiating-a-static-class-class-with-all-static-members-methods-and-variables.html)

SaravanaKumar 10-18-2004 07:27 AM

Instantiating a static class( Class with all static members - methods and variables)
 
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

Paul Lutus 10-18-2004 07:41 AM

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();


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


Tor Iver Wilhelmsen 10-18-2004 07:46 AM

Re: Instantiating a static class( Class with all static members - methods and variables)
 
saravanakumar.email@gmail.com (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.

John C. Bollinger 10-18-2004 04:34 PM

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
jobollin@indiana.edu

SaravanaKumar 10-19-2004 04:46 AM

Re: Instantiating a static class( Class with all static members - methods and variables)
 
Paul Lutus <nospam@nosite.zzz> wrote in message news:<10n6stioijj3t95@corp.supernews.com>...
> 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.

Paul Lutus 10-19-2004 04:58 AM

Re: Instantiating a static class( Class with all static members - methods and variables)
 
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


Tony Morris 10-19-2004 08:20 AM

Re: Instantiating a static class( Class with all static members - methods and variables)
 
> > > 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/





All times are GMT. The time now is 05:42 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.