Solved

constant class ?

Posted on 2002-07-14
6
245 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 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql import cannot be resolved jsp 3 111
ejb mdb examples 1 36
Selenium docs api java index 3 94
Pass multiple values or string arrays in java as a parameter 3 72
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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