Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing non-static method from static context

 
Kelly Frasure
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arthur Lyman
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lucas Richardson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic