Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global declaration of variable in android

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

like image 380
sujay Avatar asked Apr 14 '11 07:04

sujay


2 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

like image 119
Someone Somewhere Avatar answered Sep 30 '22 10:09

Someone Somewhere


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

like image 41
Hasandroid Avatar answered Sep 30 '22 11:09

Hasandroid