Solved

constant class ?

Posted on 2002-07-14
6
246 Views
Last Modified: 2010-03-31
public class  dddd()
{
   public static final String    YES            = "Y";
 
}

this class will include only constant .

Did the class need to be final , or abstract what is the best why to define a class that include only constant

thank
0
Comment
Question by:krelman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Expert Comment

by:tomboshell
ID: 7153598
This is how I would do it:


public  class MyConstant{
     private final static String YES ="Y";

     public static String getYes(){
          //quick access, only through here
          return YES;
     }
}

// and a testing class...
public class GrabConstant{
     public static void main(String[] args)
     {
          System.out.println(MyConstant.getYes());

     }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7153769
If you are unlikely to  need to subclass dddd, then you should declare the class as final, as this has less overhead in the JVM and is therefore better performing.

public final class dddd
{
  public final String  YES = "Y";
}

No accessor methods are needed here as the members are declared final and there will be no subclasses that need to modify how the values are returned.
0
 

Author Comment

by:krelman
ID: 7153865
If all the constant are - final static , therefor nobady make from them a object , are this has less overhead in the JVM too.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:s_lavie
ID: 7156665
Why don't you use interface?

public interface MyConstant
{
  public static final String YES = "Y";
}


If you are eager to use class, then I would do this:

public class MyConstant
{
 public final static MyConstant YES = new MyConstant("Y");

 private String name;
 private MyConstant(String name)
 {
   this.name = name;
 }

 public String toString()
 {
   return name;
 }
}

BTW, using that kind of class is much more type safe!
0
 
LVL 3

Accepted Solution

by:
yasser_helmy earned 50 total points
ID: 7156979
Hi everybody,
I don't think creating a public get method for the private constant is a good idea. I think it won't bring us any benefits (but I ll be glad if anybody suggests some benefits).
You may create a final class with private constructors. This will prevent creating instances of the class (which is desirable if the constants are global) and will also prevent any body from overriding these constant values.

public final class dddd{
  public static final String YES="Y";

  private dddd(){}
}
0
 
LVL 2

Expert Comment

by:flumpman
ID: 7157549
If the class will never be subclassed then declare it as final as it will have performance benefits (as mentioned).  If it is possible that it will be subclassed then leave it as final.

To prevent instantiation use a private constructor (as mentioned by yasser_helmy).

IMHO it is preferable to use a class rather than an interface.  Declaring the constants in an interface does give the "nicety" of just using the constant name without a qualifying class (or interface), however, any class that implements the interface then exposes the constants as part of its interface (affecting the OO of the system).  Personally, I would rather have a cleaner object model than a (slight) time-saving development nicety.

--
flumpman
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question