Results 1 to 9 of 9
Like Tree5Likes
  • 1 Post By Fubarable
  • 1 Post By quad64bit
  • 1 Post By Fubarable
  • 2 Post By searcher

Thread: non static method error

  1. #1
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default non static method error

    Hi

    I am getting the error " non-static method getBalance(double) can not be referenced from a static context" error when I try to
    initialze a method.


    The Method on ATM class that I am having an error with.

    Java Code:
    public void viewBalance() {
        int selection1;
        System.out.println("You have selected Balance.\n");
        System.out.println("\t-- Your Current Balance is:$ " + Account.getbalance());
        System.out.println("Return to main menu? \n [1] for YES \n");
        selection1 =input.nextInt();

    The method on the Account class I am calling.

    Java Code:
    protected  double getbalance () {
    return balance;}
    Any suggestion would be great.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: non static method error

    You're calling the getBalance method on the Account class but instead should be calling it on an Account object.

  3. #3
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: non static method error

    Quote Originally Posted by Fubarable View Post
    You're calling the getBalance method on the Account class but instead should be calling it on an Account object.

    Do I create the object on the ATM class? I tried to reference an object on Account called Acc1 using Acc1.getBalance()
    but got the error "cannot find the symbol variable Acc1". Can you clarify how to reference the Account object?

    Thanks

  4. #4
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: non static method error

    Just declare the getBalance() method static That shud solve your problem because your trying to call the method inside another method probably witch means the method your trying to call shud be a static method if you google "java static" it wil explain more thoroughly. Hope i helped
    Last edited by searcher; 01-08-2012 at 11:33 PM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: non static method error

    Quote Originally Posted by r1b View Post
    Do I create the object on the ATM class? I tried to reference an object on Account called Acc1 using Acc1.getBalance()
    but got the error "cannot find the symbol variable Acc1". Can you clarify how to reference the Account object?
    I'm not sure of your actual requirements, but I assume that they sort of model reality. A real ATM would allow a user to sign in and access one of many accounts, and so I imagine that your ATM would likely hold some collection of Account objects, perhaps an array of Account or an ArrayList of them. Then perhaps your program would allow the user to select his account and the program would assign an account variable, let's call it userAccount, to the user's account and then you would call methods off of that account variable, for example userAccount.getBalance().

    As always, the devil's in the details, meaning the specifics of your assignment requirements and the code that's present in the rest of your program.
    r1b likes this.

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: non static method error

    If Acc1 is not in the same scope as the place where you try to use it, it will not work.

    Variable Scope : Variable Scope**Language**Java Tutorial

    You have to be able to 'see' the acc1 object in order to use it. If you declare it in the ATM class body, or pass it in via a method, it'll work just fine. Don't forget, you cannot call Account.getbalance() like that, because what if there were 1 million accounts? Which one would you be getting the balance from?

    Therefor, you need to use an instance method, a method belonging to the particular instance of an Account object, not the Account class itself.
    Fubarable likes this.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: non static method error

    Quote Originally Posted by searcher View Post
    Just declare the getBalance() method static That shud solve your problem because your trying to call the method inside another method probably witch means the method your trying to call shud be a static method if you google "java static" it wil explain more thoroughly. Hope i helped
    To r1b: I know that searcher means well, but this is just what you should not do. Please do not follow his incorrect advice. You do not want to get a balance of the Account class which is what he is suggesting. Instead you want to get a balance on an individual account.
    quad64bit likes this.

  8. #8
    r1b
    r1b is offline Member
    Join Date
    Jan 2012
    Posts
    31
    Rep Power
    0

    Default Re: non static method error

    Great stuff , its working.

    Thanks

  9. #9
    searcher is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: non static method error

    True did'nt think it through your goal is to probably have an seperate object for each acount so your getter and setter shuld in that object and call the object and then the method for that object Fubarable is right
    Fubarable and r1b like this.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  2. Non-static method error
    By Lingerz in forum New To Java
    Replies: 1
    Last Post: 09-21-2009, 04:56 PM
  3. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  4. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 08:51 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •