How to call a method once everyday?

How do I call my method in Java?

  • So I have a method that takes in a integer as a menu selection, and then runs a loop based on the selection. The loop is always the same, but the menu selection changes an integer in the loop. Anyway, how do I call this method in my main method? I prompt the user to enter in a value for the menu selection, and then I want the loop to run and print out a height and a time. But when I try to call the method I get a "non-static method compute(int) cammot be referenced from a static context" error. I tried to call it as Gravity.compute(menuNumber); That might be wrong. Any help would be appreciated

  • Answer:

    Can it be your function is declared "public int compute(...)" for now? Try "public static int compute(...)" instead, that should do. Edit: If a method is not declared static, you may only call it on an instance of the class you declared this method in.

justme at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

Unless the class Gravity has been declared static, it is not an Object in RAM. In general, this is what I do... public class Gravity { double static final G; // other class variables // no constructor really needed public static void main( String[] args ) { new Gravity().run(); // makes the Object and calls its method } private void run() { do { doMenu(); int choice = scanner.nextInt(); doChoice( choice ); } while ( choice != 99 ); } private void doChoice() { ...

change your method type to static i.e. public static int compute(){....}

I suspect that Gravity is a class (rather than an object), and that compute() is not a static method. You can't call a non-static method on a class, you can only call it on an object. If it won't break the program to do so, try making the compute() method static. Otherwise, try creating an object of the class Gravity and then call compute() on that.

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.