-2

i have an activity say A that is extended by many other activities , i have a variable in activity A which is used by all other sub activities(classes) extending it. I want to know if i can set a value for that variable from a sub-activity such that the change will reflect in all the other subclasses extending Activity A. i know its a basic question ,i am new to this any help is appreciated.

eg:
Activity A has
public String global = "ABC"

Activity B extent A
display(global); ---> ABC

Activity C extent A
display(global); ---> ABC

Activity D extent A
display(global); ---> ABC  

now how can i change global in Activity D such that Activity B and C should also be affected.

Craig
  • 7,471
  • 4
  • 29
  • 46
  • 1
    If it's an *instance* variable, then any changes will only affect *that instance*. It sounds like you *may* want a static variable instead. – Jon Skeet Sep 29 '16 at 16:43
  • http://stackoverflow.com/questions/1944656/android-global-variable this solved my problem thanks for the help – Justin Babu Sep 29 '16 at 19:12

4 Answers4

1

Seems like you want a variable whos value persists and remains same throughout.

But since you only want to your inherited classes to be able to update or read the variable, you can do something like this:

class A
{
  private static int your_var = 0;

  public int get()
  {
     return your_var;
  }
  public void set(int a)
  {
      your_var = a;
  }
}

 class B extends A
  {

  }

  class C extends A
  {

  }

In your static void main:

  new B().set(101);

  new C().get()  // this will return value 101.

Note: Here only one copy of variable your_var is created. And since it is private, only the non static getter setter methods will be able to read or modify it. Thus making it accessible only by either the containing class itself or the child class objects.

Rishabh Kumar
  • 527
  • 4
  • 14
0

You need to create a static variable in Activity A.

A static variable is a variable of the class and not of the objects.

Jandisson
  • 380
  • 1
  • 10
  • i used a static variable in Activity A and changed its value in Activity B (extending Activity A) but when activity C (extending A) was called change did not get effected. – Justin Babu Sep 29 '16 at 17:40
0

try the following: in class A

public static String global = "ABC";

in class B

public class B extends A {


    public static void main(String[] args) {
    B b = new B();
    A a = new A();
    System.out.println(b.global);
    System.out.println(a.global);
    System.out.println(global);
    }

}

alternatively you could try to work with encapsulation: mark the string as private and make the "global" string available through getters and setters. This is often a more flexible solution then working with static variables

http://www.tutorialspoint.com/java/java_encapsulation.htm

0x00
  • 49
  • 4
0

You can have a singleton class which can hold global data and when need you can fetch the data from the single commonly shared instance of the singleton class. But there is another better way.

You need a class which sits on top of your activities. In any app we usually do have one such class (may be some Initilizer or main or manager class ) which wires the entities and initiates our application.

In android we have Application class which can hold global data. For reference see Android global variable

Community
  • 1
  • 1
nits.kk
  • 5,204
  • 4
  • 33
  • 55