Solved

constant class ?

Posted on 2002-07-14
6
240 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
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now