views:

192

answers:

4

Hello.

I'm developing an Android application and I'm very new on Java and Android.

I want to create some constants to use in some activities. Where can I define these constants?

Thanks.

+2  A: 

Normally, you'd use a Constants class, or define them in classes where they are used, a la:

class Constants {
   public static final int CONST_4 = 4;
   public static final String some_string_constant = "This is a constant";
}

Then you'd refer to them by:

String inst = Constants.some_string_constant;
Bill James
I know your just showing examples, but I'd like to emphasize that one should not use CONST_ as a prefix for constants. ALL_CAPS is the standard way to indicate a constant.
Skip Head
You could also make it an interface instead of a class.
Rodney Gitzel
Right, it's just an example, and is probably reminiscent of the asker's background if he comes from C/C++ (which is the feel I got off the question).
Bill James
@Rodney, use static imports instead of interfaces.
Thorbjørn Ravn Andersen
What a mess!!!! static imports, what's this?
VansFannel
@Vans This is what you get with a strongly typed language. Constants are just variables that can't change, and Java requires everything be in a class/interface.
Bill James
A: 

You can define some constants in Java enumerations.

A single Java enumerator may hold multiple fields of associated data.

Oracle provides this introduction to Java enumerations.

Andy Thomas-Cramer
Accessing enumerators is quite slow operation, compared to local variables or "static final" (which as far as I know gets inline compiled by the java compiler). Also Enumerators have a quite high overhead, when it comes to memory/space usage. See http://developer.android.com/guide/practices/design/performance.html#avoid_enums
Tseng
+3  A: 

It's considered bad practice in java, and most OO languages, to define a class simply to hold constants. It's much better to define the constants in a class they are associated with. Usually there is one. e.g.

interface MyComponent {
  /** The default height for a component */
  public static final int DEFAULT_HEIGHT = 5;
  // other stuff
}

If there really isn't one feel free to define a separate class.

EDIT:The key things here are:

  1. Make the constants easy to find. If there is a 'natural' place to put them, put them there (i.e. the default height for Component objects belong in the Component class).
  2. Don't have higher coupling than you need to. Putting all your constants in one 'Constants' class makes for high coupling, especially as subsequent modifiers tend to then put ALL constants in the Constants class, whether or not there is another class they could naturally be put in.
DJClayworth
interesting, I just read that variables and constants shouldn't be inside an interface for code readability. I agree with that, because I think good naming and a `Constants` class is much better, because most constants will be used in different classes. If you use constants to "configure" the app, its also good to have anything in one place
WarrenFaith
Variables can't be inside an interface. Constants should be, I believe, if they are relevant to all the classes implementing the interface. If your constants are used by ALL classes then yes, put them in a Constants class (as I said above). If they are used for configuring the application then maybe they belong in the Application class. They key thing is to make them easy to find, and not to have high coupling to any class. A Constants class tends to be very highly coupled if it isn't treated with care.
DJClayworth
+1 because simply true :)
WarrenFaith
+1  A: 

The most common way is to create 'constants' in the classes were you need them:

class Example { 
  private static final int FILENAME = "test.txt; 
} 

Instead of private it can also be declared default, protected or public. Although it is considered an OO anti pattern to define constants is a special 'constants' (God) class that stores constants for the whole application. Alternatively, you can also store configuration data in a Java properties file, this is not considered an anti-pattern.

Another option, that is rapidly gaining popularity, is the usage of the Dependency Inject (DI) pattern. Often this pattern is used for depend object, but it can also be used to inject constant values into objects. This can for example be implemented with Google's lightweight Guice DI framework:

class Example {
  String filename;

  @Inject
  public Example(@ConfigFilename String filename) {
     this.filename = filename;        
  }

In a special Binder class you will bind a value to the Strings annotated with @ConfigFilename. This way, you have minimal coupling and classes that can be independently tested.

Kdeveloper
If I use a properties file on Android. Where may I put it?
VansFannel
There are several ways, for example reading the property file from the classpath (directory with classes): http://www.javaworld.com/javaworld/javaqa/2003-08/01-qa-0808-property.html
Kdeveloper