1

can anyone explain to me, why i am not allowed to do the following?

public class first_class {  

    int grade1=7;
    int grade2=4;

    double average;

    public double calcAverage() {
        average=(grade1+grade2) / (2);
        System.out.println(average);
        return average;
    }

    public static void main(String []args) {
        first_class.calcAverage();
    }
}

I get the error message "non-static method calcAverage() cannot be referenced from a static context at first_class.main(first_class.java:17)".

0

3 Answers 3

2

Try this instead:

new first_class().calcAverage();

What this does is first create a new instance of your first_class class, then calls the calcAverage() method on that instance. Now, you are making a reference to the method on the instance, as opposed to trying to reference it statically.

The error is an indication that from something static (the main() method) you tried to refer to a method without an instance. You can do this if the method is marked static, but your calcAverage() is not marked static, so you need to create an instance instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed it. Sorry, was typing fast.
1

You have not instantiated an instance of first_class.

This was discussed in your previous question, and the first paragraph of my answer there still holds :) (As does my comment regarding Java naming conventions.)

2 Comments

I needed an example on how to do it in main, which caused problems to start with, but thanks, i understand it better now
@John It's not a main/not-main thing; you may use either static or instance methods from main.
0
public static void main(String[] args)
{
   first_class f=new first_class();
   f.calcAverage();
}

The method is not static. You have to access it by the object of the first_class as above. Additionally, You're not following the Java naming conventions. The class name should be FirstClassand not first_class.


If you want to access the method by its name, your code should be as follows.

package temp;

final class first_class
{
    static int grade1=7;
    static int grade2=4;

    static double average;
    public static  double calcAverage()
    {
        average=(grade1+grade2) / (2);
        System.out.println(average);
        return average;
    }
}

final public class Main
{
    public static void main(String[] args)
    {
       first_class.calcAverage();
    }
}

Make the declared fields within the class static, since static methods in Java can have access only to static members (fields or methods) and you will be able to access them using the static method as mentioned in the preceding snippet.

3 Comments

i'm trying to implement it the std way, but thanks for the other solution
@John There is no "standard"; both are legitimate. Which to use depends on your needs.
We haven't seen the use of 'final' very much thus far, so i thought it was less standard, but i guess not. Sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.