Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Send variables across classes: Java

Name: Anonymous 2013-07-26 9:06

I'm trying to store variables generated in one class to an array in another class.

i.e (pseudo code):


Class example
{
  x = 2+2;
}

Class stuff
{
  array[0] = example.x;
}


why doesn't this work.

Name: Anonymous 2013-07-26 10:12

It's typical to make variables private to their class and then use getter and setter methods.

So, like this:

Class example {

  private int x;

  public void setX(int val) {
    x = val;
  }

  public int getX() {
    return x;
  }

}

Then, in other places in code you would do

example exampleObj = new example();
exampleObj.setX(10);
System.out.println( exampleObj.getX() );

Hope this helps!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List