0

I have always had problems with accessing private varibles in a class through a method to another class, for instance now i have this problem :

i have this variable in say class Hello1 :

    private Item[][] bankTabs;

and i want to access it through another class say hello2, so i made a public method in Hello1 that is this :

    public int amountOfItemInBank(int id) {
    int amountInBank = 0;

    for(int i = 0; i < bankTabs.length; i++) {
        for(int i2 = 0; i2 < bankTabs[i].length; i2++) {
                if (bankTabs[i][i2].getId() == id)
                    amountInBank = bankTabs[i][i2].getAmount();
        }
    }
         return amountInBank;

}

but when i want to access it through Hello2, it tells me the method is not static, and when i make it static, the variable bankTabs in amountOfItemInBank do not work and i get a lot of errors.

so when i go to Hello2 class, and i try to call this method like this :

 Hello1.amountOfItemInBank(50);

how can i solve this?

Boolena
  • 225
  • 1
  • 2
  • 10
  • show us the code where you are calling the method? – anirudh Mar 26 '14 at 09:16
  • 7
    Do you understand the meaning of `static`? You shouldn't just make things `static` (or non-static) for the sake of it - think about whether a method logically acts on an instance or not. – Jon Skeet Mar 26 '14 at 09:18
  • You probably are filling `bankTabs` on initialization of the `Hello1` class. Seeing how it tells you the method is not static, you're probably trying to call this method on the class, rather than on an object, which is not going to work. – gpgekko Mar 26 '14 at 09:19
  • @JonSkeet so how will i access a private variable in a class in another class? – Boolena Mar 26 '14 at 09:19
  • 5
    You didn't answer my question: do you understand the meaning of `static`? What is your `Hello1` class meant to represent? If it's a bank, why isn't it called `Bank`? And what would you expect to happen if you had two different banks? Surely they'd have different numbers of items... Once you start trying to think logically about what your types mean, this sort of thing often drops out. Don't concentrate on your immediate compile-time problem - think more about your types. – Jon Skeet Mar 26 '14 at 09:21
  • @Boolena You're not accessing a variable in a `class`, you want to access a variable in an `object`. You have these two mixed up. – gpgekko Mar 26 '14 at 09:21
  • You have public method amountOfItemInBank() in Hello1 class which is all well and good. The problem is you must instantiate an instance of Hello1 and call that public method through that instance. You cannot call it Hello1.amountOfItemInBank(). – anonymous Mar 26 '14 at 09:22
  • possible duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) – user207421 Mar 26 '14 at 09:22

2 Answers2

2

Either make an object of Hello1 class and then access the method

Hello1 obj = new Hello1();
int returnValue = obj.amountOfItemInBank(50);

or declare both the variable bankTabs and method amountOfItemInBank as static in Hello1 class and use Hello1.amountOfItemInBank(50); as you did earlier.

Also, read more here Understanding Class Members to clear your understanding and then you can solve the problem for once and for all.

AKS
  • 18,983
  • 3
  • 43
  • 54
  • glad that it met your expectations. – AKS Mar 26 '14 at 09:28
  • 1
    @Boolena "This was my answer, using objects". I don't want to be mean, but it is pretty easy to reason that using objects solves many issues when you use an object oriented programming language ;) – Gimby Mar 26 '14 at 09:59
0

Static METHODS can be called on class, and not object, like

Hello1.amountOfItemInBank(50);

To call a non-static method, you need an object of a class:

Hello1 hello = new Hello1();
hello.amountOfItemInBank(50);

The method doesn't have to be static to make use of a static field in such way. Declaring a field as static lets you use its value (if it's public) without making object of a class:

Item[][] items = Hello1.bankTabs;

or by a method call (if it's private):

Hello1 hello = new Hello1();
Item[][] items = hello.getBankTabs();


// in your class
private static Item[][] bankTabs;
public Item[][] getBankTabs() {
    return bankTabs;
}

If you do not need to access the field without instantiating the class, you probably do not want to make that variable static.

mareckmareck
  • 1,560
  • 13
  • 18