I am new to android development, I am parsing a xml file through SAX parser and storing the parsed data into a string.Now i need to use that string in another class, so i need to know how to call that parser in the new class. thanks in advance
-
Note that global data is a bad idea unless it's constant. – the_drow Apr 14 '11 at 07:41
-
Without knowing your class hierarchy in more detail, I'm wondering what's preventing you from using basic getter method? – harism Apr 14 '11 at 08:29
5 Answers
I always make a class that contains all of my globals and call it "Constants.java"
final public class Constants//final to prevent instantiation
{
public static final String SOME_STRING = "0.04";
public static final int SOME_NUMBER = 5;
public static final float METERS_PER_MILE = 1609.344f;
//private constructor to prevent instantiation/inheritance
private Constants()
{
}
}
to use one of these in your code, be sure to import the class and use:
Constants.SOME_NUMBER

- 23,475
- 11
- 118
- 166
-
-
because "an interface is a group of related methods with empty bodies" – Someone Somewhere Apr 14 '11 at 17:51
-
eventhough "an interface definition may also define properties that are automatically public static final", I consider the main purpose of an interface to represent a contract of related methods with empty bodies. – Someone Somewhere Apr 14 '11 at 18:01
You can use a static variable and can access it any where in the application
public static int myVar = 1;
access it by ClassName.myVar

- 336
- 5
- 12
You can use intent to pass the value. Also,you can use the manifest.xml to make a gloable variable.

- 98
- 1
- 8
Using the android application extension is NOT multi-process safe, as described here: How to declare global variables in Android? Note the first response. He defines how to extend the application itself but makes a note that "this method offers no way easy way of persisting the global state. If your application finds this necessary you should be using some sort of store; see the Android docs for a variety of methods." I have also seen other posts that state that between processes this method needs to be modified slightly, but I think it's possible. Let me know if I understood that wrong...
You should use application object to get a global variable, you can see a working example here http://www.helloandroid.com/category/topics-covered/application-object
Application class documentation is stated here http://developer.android.com/reference/android/app/Application.html

- 4,102
- 3
- 21
- 19