Im wondering how can I read a variable from one class’s to another?
I have two class’s, triangle.java and screen.java
in triangle.java I have: public double bound=0;
how to I read that variable in Screen.java?
triangle.java:
public class triangle extends Activity
{
public EditText txtbound;
public double bound = 0;
txtbound = (EditText)findViewById(R.id.txtbound);
public double calculate()
{
bound=Double.parseDouble(txtbound.getText().toString());
// do maths here
}
}
and Screen.java has:
double bound1 = triangle.bound;
this doesnt work, how to I get the variable in Screen.java? thanks!
make bound1 private in Screen class like
Use getters/setters in Screen like
In Triangle acquire instance of Screen:
It’s a convention to name your classes starting with capital letters.
You also don’t have to initialize your class fields when declaring them. Better
use constructors for that.
I suggest you read this tutorial to get more familiar with classes, fields and variables. Will save you time in the long run.