Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is best way to define constants in android, either static class, interface or xml resource?

I'm developing an android application which uses web service to get data from server, for that I'm having three different set of URLs to point development system, test server and live server. It's difficult to change URL whenever I want to give application for testing/live. so I planned to make it as configurable, so that application can get appropriate URL based on me build type configuration constant. So,

  • which is the best way to keep this constants, java static class or java public interface or xml resource file.? When? Why?
  • which gives better performance?, When? Why?

Ex: xml resource

<integer name="config_build_type">0</integer> <string-array name="url_authentication">     <item >http://development.com/xxxx</item>     <item >http://test.com/xxx</item>     <item >http://example.com/xxx</item> </string-array> 

Java static constant

public class Config {     public static final int BUILD_TYPE = 0; // 0 - development, 1 - test, 2 - live     public static final String[] URL_AUTHENTICATION = {"http://development.com/", "http://test.com/", "http://example.com"}; } 
like image 356
Jayabal Avatar asked Jun 22 '12 05:06

Jayabal


People also ask

Should constants be in interface or class?

Java programmers commonly define constants inside their interfaces, if it makes design sense. You can do so using variables in an interface because the values will be present instantly at runtime and their values shared among all classes implementing your interface, because they are static and final.

Where are constants stored android?

Create a class constants in your base package folder. Fill it with public static final values. Moreover, both the class as well as the interface can also be declared as abstract .

What is constant in Android?

Magic constants are a response to the ever-growing number of APIs in the Android framework, where the state, type of a component, or an action are chosen by providing an int or String constant. For example, setting view visibility with int constants or requiring a system service with String constants.


1 Answers

There is a big difference between the two in that you can reference project resources in your XML layouts. They are available in the application context and are therefore accessible across the global application. The biggest advantages of using project resources is the ease of access and that they allow you to organize your project significantly.

static final constants are compiled into the java bytecode; project resources are compiled into a binary format within the apk. Accessing either is extremely efficient... if there is a difference between the two, it is trivial at most.

There isn't a set rule on how you should be using resources/constants in your project. That said, I personally use resources for values that I might need to use in my XML or java code. On the other hand, I typically use static final constants for values that will only be used by my java code and are specific to my implementation.

Also note that it is possible to load XML resources at runtime depending on the device's current configuration (i.e. screen size, locale, etc.). So you should take this into consideration when deciding whether or not you should declare the constant in XML or directly in your .java files.

like image 185
Alex Lockwood Avatar answered Sep 26 '22 21:09

Alex Lockwood