• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How do you pass variables between classes?

Why per Intents?
You can pass variables by creating an objekt of another class...
Like here:

public class Main {

static Test t = new Test();
public static void main(String[] args){
t.Set(3);
System.out.println(t.Get());
}

}

extra file:

class Test{
private int i;
public int Get(){
return i;
}
public void Set( int a ){
this.i = a;
}
}

I hope that helps

BTW: There is no main in android, so just do this without an main...
in any class you wish...

MFG Da Wolf
 
check out this I had a question on this, and posted the Idea I had, so if it comes to be its a good idea, or you try it out, let me know what you think, hopefully by tomorrow someone'll tell me if i have an obvious flaw in this idea
 
To pass variables between classes you use getters and setters.

Examples
Code:
class Rectangle {
  int x;
  int y;
  int width;
  int height;

  public int getX() {
    return this.x;
  }

  public int getY() {
    return this.y;
  }

  public int setX(int x) {
    this.x = x;
  }

  public int setY(int y) {
    this.y = y;
  }

  // etc etc
}

// In another class you'd have:

Rectangle r1;

r1.setX(50);

int example = r1.getX();
 
Back
Top Bottom