bluebl1
bluebl1

Reputation: 176

How do I create an int that other classes can see and edit? Android

I have a class called Scouting, and it runs a function in a different class ScoutingFormData(different java file in the same package). I want it so that an integer defined in Scouting can be edited from ScoutingFormData. I defined the int:public int SFID=-1; in the main class of Scouting, but I can't figure out how to edit that int from ScoutingFormData.

Upvotes: 1

Views: 64

Answers (3)

barto90
barto90

Reputation: 689

Use getters and setters and avoid using public attributes.

Make these methods in your Scouting class:

public int getMyInteger() 
{
  return myInteger;
}

public void setMyInteger(int newIntegerValue) 
{
  this.myInteger = newIntegerValue;
}

Where you have your private int myInteger.

In your ScoutingFormData class your can get and set the values:

setMyInteger(23); // The integer myInteger in the Scouting class is now set to 23

int newInteger = getMyInteger(); // The integer newInteger has been initialized to myIntegers value

Upvotes: -1

Frank
Frank

Reputation: 15641

Don't make your instance fields public, use getter and setters.

public int getField() {
    return field;
}
public void setField(int field) {
    this.field = field;
}

This is if your field needs to be an instance field.

If you need a field that belongs to the class ScoutingObject you need to make it static

public static int SFID=-1;

Then you can access it like this:

ScoutingObject.SFID

Upvotes: 2

wtsang02
wtsang02

Reputation: 18873

add static modifer to it so it belongs to the class.

If you mean objectwise. Use getters and setters.

Or you can change it directly by doing ScoutingObject.SFID=?; //in your ScoutingFormData class.

Upvotes: 2

Related Questions