43

Possible Duplicate:
What is the reason behind “non-static method cannot be referenced from a static context”?
Cannot make a static reference to the non-static method
cannot make a static reference to the non-static field

I am not able to understand what is wrong with my code.

class Two {
    public static void main(String[] args) {
        int x = 0;

        System.out.println("x = " + x);
        x = fxn(x);
        System.out.println("x = " + x);
    }

    int fxn(int y) {
        y = 5;
        return y;
    }
}

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static method fxn(int) from the type Two

Community
  • 1
  • 1
kunal
  • 477
  • 1
  • 4
  • 4
  • 1
    Not your down-voter, but you will want to go through [the Java tutorials](http://docs.oracle.com/javase/tutorial/reallybigindex.html) or a basic textbook to learn the rudiments of Java. – Hovercraft Full Of Eels Jul 15 '12 at 12:12
  • 8
    Don't down-vote this question if you don't know about the situation I am in right now. :/ I'm still on the 4th chapter of Head First Java, and was confused by a statement about return. I was just trying to do that. – kunal Jul 15 '12 at 12:36
  • You should have searched SO for an answer before asking the question. – Stephen C Jul 15 '12 at 13:03
  • @Stephen - I searched. But it didn't answer my query – kunal Jul 15 '12 at 13:34
  • @Kunal - I get to differ. The second "possible duplicate" matches your title string very closely, and directly answers your question. If you didn't find it, you didn't look properly. If you did find it, you didn't read it properly. – Stephen C Jul 16 '12 at 01:15

4 Answers4

72

Since the main method is static and the fxn() method is not, you can't call the method without first creating a Two object. So either you change the method to:

public static int fxn(int y) {
    y = 5;
    return y;
}

or change the code in main to:

Two two = new Two();
x = two.fxn(x);

Read more on static here in the Java Tutorials.

Keppil
  • 45,603
  • 8
  • 97
  • 119
5

You can't access the method fxn since it's not static. Static methods can only access other static methods directly. If you want to use fxn in your main method you need to:

...
Two two = new Two();
x = two.fxn(x)
...

That is, make a Two-Object and call the method on that object.

...or make the fxn method static.

Magnus Winter
  • 921
  • 9
  • 20
5

You cannot refer non-static members from a static method.

Non-Static members (like your fxn(int y)) can be called only from an instance of your class.

Example:

You can do this:

       public class A
       {
           public   int fxn(int y) {
              y = 5;
              return y;
          }
       }


  class Two {
public static void main(String[] args) {
    int x = 0;
    A a = new A();
    System.out.println("x = " + x);
    x = a.fxn(x);
    System.out.println("x = " + x);
}

or you can declare you method as static.

Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
0
  1. A static method can NOT access a Non-static method or variable.

  2. public static void main(String[] args) is a static method, so can NOT access the Non-static public static int fxn(int y) method.

  3. Try it this way...

    static int fxn(int y)

    public class Two {
    
    
        public static void main(String[] args) {
            int x = 0;
    
            System.out.println("x = " + x);
            x = fxn(x);
            System.out.println("x = " + x);
        }
    
        static int fxn(int y) {
            y = 5;
            return y;
        }
    

    }

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75