File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing non-static method from static context" Watch "accessing non-static method from static context" New topic

accessing non-static method from static context

Kelly Frasure

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\ non-static method getAccountNumber() cannot be referenced from a static context
A:\BankProgram\ non-static method deposit(double) cannot be referenced from a static context
2 errors
Tool completed with exit code 1
//Kelly Frasure
//CS 162 - Assignment 6.7

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

Joined: Jul 28, 2001
Posts: 260
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:
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
Arthur Lyman

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>
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:
subject: accessing non-static method from static context
It's not a secret anymore!