Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes accessing non-static method from static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing non-static method from static context" Watch "accessing non-static method from static context" New topic
Author

accessing non-static method from static context

Kelly Frasure
Greenhorn

Joined: Mar 03, 2001
Posts: 15
Here's the problem I hope someone can help. I'll just post the code and the error message, and maybe someone can lead me in the right direction.
-----------------------------------------------------------------
A:\BankProgram\BankTeller.java:61: non-static method getAccountNumber() cannot be referenced from a static context
Account.getAccountNumber();
^
A:\BankProgram\BankTeller.java:62: non-static method deposit(double) cannot be referenced from a static context
Account.deposit(getAmount);
^
2 errors
Tool completed with exit code 1
-----------------------------------------------------------------
//**************************************************************
//Kelly Frasure
//CS 162 - Assignment 6.7
//BankTeller.java
//**************************************************************


[This message has been edited by Cindy Glass (edited July 28, 2001).]
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
hey,
if i got right , Account is not an instance reference but a class name. If so, you can't run a non-static method with this syntax:
Account.method();
so choose one: make your method static if you need so, or build an instance of Account and run your method with it.
just try it.


Asher Tarnopolski
SCJP,SCWCD
Arthur Lyman
Greenhorn

Joined: Dec 30, 2000
Posts: 11
I am guessing that the methods under the class Account are somehow part of Bank, and should be referenced by the specific instance of Bank, JavaBank.


Arthur E. Lyman<BR>a_lyman@hotmail.com
Lucas Richardson
Ranch Hand

Joined: Jul 08, 2001
Posts: 32
Asher has it right, though you'll want to create an instance of account (or else you can only ever have one account, everything in the class Account that you're accessing would have to be static). Anything that isn't declared static requires an instance of the class to be created before you can access it because each instance gets it's own copy.
You might also want to use different names for some things. For instance:
double getFunds
sounds like a method, but it isn't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: accessing non-static method from static context
 
Similar Threads
Problem compiling java class
Stumped, need inspiration.
Can't figure out why variable isn't recognized
SubClass Blues!
Printing out of an data stored in and array and SringBuffer array