Results 1 to 9 of 9
5Likes Thread: non static method error
- 01-08-2012, 11:08 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
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.
Any suggestion would be great.Java Code:protected double getbalance () { return balance;}
-
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.
- 01-08-2012, 11:29 PM #3
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
- 01-08-2012, 11:30 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
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.
-
Re: non static method error
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.
- 01-08-2012, 11:34 PM #6
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.
-
Re: non static method error
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.
- 01-08-2012, 11:39 PM #8
Member
- Join Date
- Jan 2012
- Posts
- 31
- Rep Power
- 0
Re: non static method error
Great stuff , its working.
Thanks
- 01-08-2012, 11:39 PM #9
Member
- Join Date
- Jan 2012
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Non-static method error
By Lingerz in forum New To JavaReplies: 1Last Post: 09-21-2009, 04:56 PM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks