I have a Boolean large small and medium in classB. ClassA wants to use it, as well as classB. If I try putting the Boolean in classA it changes to static and fails. Can someone tell me what I should use? I don't know what abstract does yet.
Asked
Active
Viewed 293 times
-4
-
[What does the `static` keyword do in a class?](http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – Sotirios Delimanolis Apr 29 '14 at 02:58
-
[Understanding class members.](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) – Sotirios Delimanolis Apr 29 '14 at 02:59
-
Could you post some code for us to see? – Alejandro Apr 29 '14 at 03:14
1 Answers
1
I'm not sure what you mean by "changes to static", but if you want to access the value of an instance variable from another class, you should declare it private and create an accessor method for it. Given your example:
public class classB {
private boolean large, medium, small;
public boolean isLarge() {
return large;
}
public boolean isSmall() {
return small;
}
public boolean isMedium() {
return medium;
}
}

wickstopher
- 981
- 6
- 18
-
Good point about the use of private variables. However, I don't think he's working with instances though, just based on the structure of his question. It looks like he wants to do ClassA.SOMETHING which is where he's getting `static` from. – Alejandro Apr 29 '14 at 03:14
-
1Having booleans for small, medium and large sounds like it calls for instances, although without code it's impossible to say. If he's not using them, perhaps he should be! – wickstopher Apr 29 '14 at 03:18
-
1Having three booleans sounds like a *bad design*. An `enum` is a better choice, or even a set of constant `String`s/`int`s to compare against. – Brian Roach Apr 29 '14 at 03:26