-4

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.

Mr.CodeIt
  • 17
  • 1
  • 5

1 Answers1

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
  • 1
    Having 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
  • 1
    Having 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