-3

I have 2 classes that are not the main classes, but I want class B to access the variable from class A. I am not sure how to do it.

public class A (
    public int humanCakes;

    //This method is called by somewhere, when it is called it adds one
    public void humanCakes() {
        huamnCakes ++;
    }
 )

public class B {
    public void addUp() {
        Cakes = humanCakes + 4;
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • You forgot to define your variables. As written your code won't compile/ – PM 77-1 Apr 04 '17 at 22:57
  • Possible duplicate of [How do getters and setters work?](http://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – Ousmane D. Apr 04 '17 at 22:58

1 Answers1

0

Since your variable in class A is public you can do the following

public class B{
    int Cakes;

    public void addUp(A obj)
    {
      Cakes = obj.humanCakes + 4;
    }
}

Note that you must past the A object into the addUp method in order to get the reference to the humanCakes variable

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26