-1

I have a variable which is initialize in one class and i want to use it in another java class and i want to use collect as table name in another class which is for Database Helper class how can i do it.. Thanks in advance for making a time to read it :) i have a example code below

public class example()
{
String collect;
//and here i have one spinner 
//and in itemSelected in spinner
//i getting that item like this
String item = getItemslected.toString;
collect=item;
}
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
Badprince
  • 19
  • 4

4 Answers4

1

Options:

1.Use static variable:

Declare static String collect; and access it from other class as <YourClassNmae>.collect; where YourClassName is the class in which you have declared the static variable.

2.Use Application class

Create application class extending Application

public class MyApplication extends Application {

    private String someVariable;

    public String getSomeVariable() {
        return someVariable;
    }

    public void setSomeVariable(String someVariable) {
        this.someVariable = someVariable;
    }
}

Declare the application class name in manifest like:

<application 
  android:name=".MyApplication" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">

Then in your activities you can get and set the variable like so:

// set
((MyApplication) this.getApplication()).setSomeVariable(collect);

// get
String collect = ((MyApplication) this.getApplication()).getSomeVariable();
kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • just to know, extending application can slow runtime if used to much or it's the same as using static variables? – Pier Giorgio Misley Sep 27 '16 at 08:02
  • 1
    Makes no big difference : http://stackoverflow.com/questions/10844492/static-variables-vs-application-variables – kgandroid Sep 27 '16 at 08:05
  • i am doing in this android if i declare a static variable in main activity and if use it in another class my database class to create a table like mainactivity.mystatic variable :) – Badprince Sep 27 '16 at 10:15
0

You can declare that variable as public static and you can use it from any other class. Other way to use that is by using set and get method.

Vikash Kumar Verma
  • 1,068
  • 2
  • 14
  • 30
0

You can make a variable static and refer to it using Classname.variable. If you don't want to make it static, you'll need a reference to an instance of the class then refer to it using myInstance.variable. The other option is to use methods to return it (again, either static or non-static).

The variable (or method) will also need the appropriate access modifier: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Zarwan
  • 5,537
  • 4
  • 30
  • 48
0
public class Main{
     public static void main(String[] args) {
         System.out.println(Example.test);
         Example.test = "123";
         System.out.println(Example.test);
    }
}

public class Example{
     public static String test = "This is a Test";
}

Output:

This is a test
123
CloudPotato
  • 1,255
  • 1
  • 17
  • 32