3 Replies - 1976 Views - Last Post: 02 November 2008 - 05:39 PM Rate Topic: -----

#1 jaxxy89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-September 08

non-static method being called from a static context

Posted 02 November 2008 - 12:04 AM

Ok so I've got a method that runs a textual UI with the output window, and I need to call a method which returns an object from another class, but when i compile it says it cant call a non static method from a static context, but I'm unsure as to what is causing my class to be static, so how would i fix it?

 /**
     * The run method controls the input and output of the bank system.
     */
    public void run()
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        Scanner inputReader = new Scanner(reader);
        boolean isOpen = true;
        while (isOpen == true){
            listMenu();        
            System.out.println("Enter Option:");
            int input = inputReader.nextInt();
            if (input == 1){
                System.out.println("Enter account name followed by amount to deposit:");
                String getName = inputReader.next();
                Object acctDep = Bank.get(getName);
                int depAmt = inputReader.nextInt();
                ((Account)acctDep).deposit(depAmt);
//                 Object acctDep = Bank.get(inputReader.next());
//                 acctDep.deposit(inputReader.nextInt());
            } else if (input = "2"){
                System.out.println("Enter account name followed by amount to withdraw:");
                String getName = inputReader.next();
                Object acctWit = Bank.get(getName);
                int witAmt = inputReader.nextInt();
                ((Account)acctWit).withdraw(witAmt);
//                 Account acctWith = Bank.get(inputReader.next());
//                 acctWith.withdraw(inputReader.nextInt());
            } else if (input = "3"){
                Bank.listAccounts();
            } else if (input = "4"){
                //code to charge annual fee
            } else if (input = "5"){
                System.out.println("Exiting...");
                Bank.toFile();
                isOpen = false;            
            } else {
                System.out.println("Input Error, please re-input selected option as a single digit only (ie no spaces)");
            }
        }
        System.exit(0);
    }


The code it highlights when i compile is Object acctDep = Bank.get(getName);

This post has been edited by jaxxy89: 02 November 2008 - 12:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: non-static method being called from a static context

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: non-static method being called from a static context

Posted 02 November 2008 - 12:15 AM

which part is giving you the error, you cannot access an instance variable or call an instance method from a static method....check your code or specify which line is giving you the error
Was This Post Helpful? 0
  • +
  • -

#3 jaxxy89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 24-September 08

Re: non-static method being called from a static context

Posted 02 November 2008 - 12:20 AM

View Postmostyfriedman, on 2 Nov, 2008 - 12:15 AM, said:

which part is giving you the error, you cannot access an instance variable or call an instance method from a static method....check your code or specify which line is giving you the error

sorry the code it highlights when i compile is Object acctDep = Bank.get(getName);
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: non-static method being called from a static context

Posted 02 November 2008 - 05:39 PM

View Postjaxxy89, on 2 Nov, 2008 - 12:20 AM, said:

View Postmostyfriedman, on 2 Nov, 2008 - 12:15 AM, said:

which part is giving you the error, you cannot access an instance variable or call an instance method from a static method....check your code or specify which line is giving you the error

sorry the code it highlights when i compile is Object acctDep = Bank.get(getName);


So you have to have a class Bank
Which has a static method named get
That receives a String has parameter an return an object:

class Bank {
	...

public static Object get(String str) {
   .....
   return anObject;
}



if your get method is not static you'll have to create a Bank object first

Bank bank = new Bank(...)
Object obj = bank.get(next);;

This post has been edited by pbl: 02 November 2008 - 05:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1