Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

constant class ?

Posted on 2002-07-14
6
Medium Priority
?
249 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
Industry Leaders: 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 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

609 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