1

I am working in android. I have two acitivities in my project. I have declared a public static variable in one activity like this:-

public static String name="KUNTAL";

In my second activity i am trying to use this variable, then it is generating error that this name variable is not exist.

Is this possible to use a variable anywhere in my project if it is declared as public ?

Please suggest me what mistake i have done.?

Thank you in advance...

grunk
  • 14,718
  • 15
  • 67
  • 108
Pushpendra Kuntal
  • 6,118
  • 20
  • 69
  • 119

6 Answers6

14
public class Activity1 extends Activity {

    public static String name="KUNTAL";  //declare static variable. 

    @Override
    public void onCreate(Bundle savedInstanceState) {

    }
}

public class Activity2 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
         Activity1.name; //way to access static variable using dot operator.
    }
}
Krishnendu
  • 1,289
  • 18
  • 26
  • is there any performance problem (with activities) for doing this? – CQM Sep 08 '14 at 20:32
  • i don't think there will be a problem. but it's a good idea to use intents for passing parameters between activities, rather than static variables. – Krishnendu Sep 10 '14 at 06:18
  • 3
    @CQM: If the static variable is also `final` (does/can never change), there will be definately no performance problem, since the compiler already inserts the *value* instead of a reference to the variable. If it is only `static` and not `final`, it still would not be a problem for performance though, since it will be treated similar to a "global variable" from other languages. – Levite Feb 27 '15 at 09:51
  • If you understand how static declarations work then you will understand how bad this practice is given that we have intents to pass values from one activity to another. – frogEye Apr 04 '16 at 05:48
7

I think you must access them in a 'static way', i.e.:

String myVar= name; // wrong
String myVar= TheClassThatContainsName.name; // right
Laurent'
  • 2,611
  • 1
  • 14
  • 22
4

You can use the variable specified as public static in any Activity but you need to access that variable by using the Activity name where you Declared it.

For Accessing in Second Activity just use ;

Activity1.name ="Me";

means that name variable belongs to Activity1 and you are using in Acvity2

Venky
  • 11,049
  • 5
  • 49
  • 66
1

Declaring variables public static is not the recommended way of passing values between two activities. Use Intent object instead:

public class Activity1 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
   yourButton.setOnClicklistener(listener);
  }
}

//On click of some button start Activity2:

View.onClicklistener listener = new View.OnClickListener(){

   @Override
    public void onClick(View v) {

      Intent mIntent = new Intent(Activity1.this,Activity2.class);
      mIntent.putExtra("yourKey","yourValue");
      startActivity(mIntent);

     }

};

//and then in your activity2 get the value:

public class Activity2 extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

   String yourValue = getIntent().getExtras().getString("yourKey");

  }
}
Pankaj
  • 2,115
  • 2
  • 19
  • 39
1

if you're using the same variable in more than one activity then make a class something like ActivityConsts{}, and declare and initialize this variable in there(in ActivityConsts). And access this variable from anywhere with the class name. ex-

declare a class-
public class ActivityConsts {
//your String var
public static String name="KUNTAL";
}

now in your activity:

public class MyActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  String yourStringVar = ActivityConsts.name;
 }
}
0

There is no datatype specified for your variable. Use

public static String name="KUNTAL"; 
blessanm86
  • 31,439
  • 14
  • 68
  • 79