问题描述:

This question already has an answer here:

  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers

网友答案:

You need to add "static" to all your methods, as well as to the variables you defined at the beginning (the arrays "rain" and "values").

You should always consider setting your methods as static whenever you're not using properties defined in the class you're working with (e.g. a method to calculate the average, usually depends only on the values you get as argument).

网友答案:

Instance variables cannot be accessed in static methods such as main. You must make all of the fields and methods static.

static String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values;
public static String mostRain(...
public static String leastRain(...
网友答案:

Your problem is that your main method is static but the other methods and variables are not.

You either need to create an instance of rainfall and call the methods in that instance or make all the methods and variables static too.

网友答案:

You are calling a method in a static context:

System.out.println(Average(...));

That means that you are using the method without using it within an object context, i.e. object.Average(...).

You must either

  • Declare the method to be static:

    public static double Average(...) {

  • Or use the method on an object:

    YourObject obj = new YourObject(); obj.Average(...);

PS: Methods should start with a lowercase character.

网友答案:

Change this

String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

to this

static String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

or

put the (non-static) variables into the main function itself, and then pass them into the functions as needed. Such as

public static final void main(String[] ignored)  {
   String[] rain =  { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
   double[] values = { 0.40,0.94,3.21,3.74,1.73,1.03,1.27,2.58,6.98,6.90,2.80,2.53};

   Total(values);

All functions called by main, as stated by others, either need to be static as well, or alternatively called via a new instance.

(And please, change function names to lowercase.)

网友答案:

Since those methods are non static, they can only be accessed through an object of that class. When you run static methods, they are not run through an instantiated object so they don't have access to the non static methods.

So what you would do is create a Rainfall object in main and call the methods on that object. This is how is is usually done. Generally, the main() method doesn't do much work itself but creates an object and calls the methods on that object to do the actual work.

So you would do something like:

public class Rainfall {
    //your methods from above

    public static void main(String[]Args) {
        String highest = "" ;
        String least = "";
        double total = 0 ;
        double average = 0 ;
        double high = 0;
        double low = 0;

        Rainfall rain = new Rainfall();
        System.out.println(" AUSTIN Tx RAINFALL 2009 " );

        for( int i = 0 ;  i < rain.length ; i ++ ) {
            System.out.println(rain[i]+"\t"+values[i]);
        }

        System.out.println(rain.Total(total));
        System.out.println(rain.Average(average));
        System.out.println(rain.mostRain(highest,high));
        System.out.println(rain.leastRain(least,low));

    }

}
相关阅读:
Top