2

I do not quite understand the use of "static" properly.

I have a class which includes variables that I want to access from a different class.

However, when I try to access this getter method from the different class I get an error stating:

"non-static method getAccNumber() cannot be referenced from a static context."

So, how can I find this variable's value without making it static. The problem with this is if I make it static, every instance of this object overwrites the previous value. So they all end up with the same account number in this case.

Let me explain in more detail:

I have a Class called Account, which contains a variable called accountNumber, and a getter method called getAccNumber(). I have a second class called AccountList which is a separate arraylist class to store instances of Account. I want to create a method to remove an element based upon its accountNumber. So I'm searching and using getAccnumber() within the AccountList class to compare with a user parameter and removing if correct! But I can't use this method without making it static!! Any help/explanation would be greatly appreciated :)

This is what I am trying to do:

public boolean removeAccount(String AccountNumber)

{   


    for(int index = 0; index < accounts.size(); index++)

    {

    if (AccountNumber.equals(Account.getAccNumber()))
   {

        accounts.remove(index);
        return true;
    }
}
    return false;
}

Thank you!

3
  • 4
    Each instance will have a separate set of instance variables... so which instance are you trying to access? To give you an example, suppose your "other class" is the tax office... does it make sense to say "What is person's salary?" No... it has to say "What is the salary of this specific salary?" If the tax office has no way of knowing about you, they can't find out your salary... (And no, this isn't trying to describe a tax avoidance scheme.) Commented Nov 12, 2014 at 17:22
  • @JonSkeet It may not be trying to, but it... hrm... isn't, I guess! Commented Nov 12, 2014 at 17:25
  • other class is called Account, i'm trying to access accountNumber from this class. but want to use it's method to do so! And yeah yeah, you'll have people watching you very closely talking like that ;) Commented Nov 12, 2014 at 17:26

3 Answers 3

3

Let's take an example where you have

public class A {
  static void sayHi() { System.out.println("Hi"); 
  //Other stuff
}

and

public class B {
  void sayHi() { System.out.println("Hi"); 
  //Other stuff
}

Then

public class C {
  public C() {
    A.sayHi(); //Possible since function is static : no instantiation is needed. 
    B.sayHi(); //Impossible : you need to instantiate B class first
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You can check out this link for a short definition:

Static Method Definition

If you declare a variable as static, it will not be unique for each instance of the class. If you declare the variable as private only, then you can create getter and setter methods that will allow you to access the variable after you have created an instance of the class. For example, if you have classA and classB and you are working in classB and want the private int size of classA:

classA a = new ClassA();

int size = a.getSize(); //getSize() returns the private int size of classA

2 Comments

Says no constructor for Account? (classA in your example)
Do you have a constructor for your Account class? public Account(){} would be the default. You can also pass in any parameters you may need and in the body set them equal to private variables of the account class.
0

A static variable is one that lives with the class itself. A non-static variable is unique to each instance of that class (i.e., each object built from that class.) A static variable you can access as a property of the class, like:

SomeclassIMadeUp.numberOfFish;

so if you change the numberOfFish property for that class, anywhere you reference it, you see the change (as you've noticed.)

To have one unique to each instance, don't declare the variable as static and then add a getter and/or setter method to access it.

like

private int numberOfFish;

public int getNumberOfFish() { return (numberOfFish); }

So to your question...

Ocean pacific = new Ocean();
pacific.water = Ocean.salty;  // <-- Copying the value from a static variable in the class Ocean
                              // to the instance variable in the object pacific (which is of Ocean class).
pacific.setNumberOfFish(1000000000);

Octopus o = new Octopus();
o.diningOpportunities = pacific.getNumberOfFish();  // <-- Calls the public method to return a
                              // value from the instance variable in the object pacific.

2 Comments

yeah i totally get that! you're right, but i want to access it from outside of the class itself! a different class.
You access it by the public method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.