开发者

Non-static method cannot be referenced from static content

开发者 https://www.devze.com 2023-02-06 01:35 出处:网络
I cannot compile the following code: public class Test { public static void main (String [] args ){ int a = calcArea(7, 12);

I cannot compile the following code:

public class Test {
 public static void main (String [] args ){
  int a = calcArea(7, 12);
  System.out.println(a);
 }

 int calcArea(int height, int width) {
  retu开发者_开发知识库rn height * width;
 }
}

The following error appears:

Non-static method calcArea(int, int) cannot be referenced from static content

What does it mean? How can I resolve that issue..?

EDIT:

Based from your advice, I made an instance which is new test() as follows:

public class Test {
    int num;
    public static void main (String [] args ){
        Test a = new Test();
        a.num = a.calcArea(7, 12);
        System.out.println(a.num);
    }

    int calcArea(int height, int width) {
            return height * width;
    }

}

Is this correct? What is the difference if I do this...

public class Test {
 public static void main (String [] args ){
  int a = calcArea(7, 12);
  System.out.println(a);
 }

 static int calcArea(int height, int width) {
  return height * width;
 }
}


Your main is a static, so you can call it without an instance of class test (new test()). But it calls calcArea which is NOT a static: it needs an instance of the class

You could rewrite it like this I guess:

public class Test {
 public static void main (String [] args ){
  int a = calcArea(7, 12);
  System.out.println(a);
 }

 static int calcArea(int height, int width) {
  return height * width;
 }
}

As comments suggests, and other answers also show, you might not want to go this route for evertyhing: you will get only static functions. Figure out what the static actually should be in your code, and maybe make yourself an object and call the function from there :D


What Nanne suggested is definitely a fix for your problem. However, I think it would be prudent if you got in to the habit right now, while you are early in your progression of learning java, of trying to use static methods as little as possible, except where applicable (utility methods, for instance). Here is your code modified to create an instance of Test and call the calcArea method on your Test object:

public class Test {
 public static void main (String [] args ){
  Test test = new Test();
  int a = test.calcArea(7, 12);
  System.out.println(a);
 }

 int calcArea(int height, int width) {
  return height * width;
 }
}

As you get further in to coding with java and, presumably given the code you've just written start dealing with objects such as polygon objects of some sort, a method like calcArea belongs at the instance context and not the static context so it can operate on the internal state of your objects. This will make your code more Object Oriented and less procedural.


calcArea mustn't be static. For using another methods in main class, you must create an instance of its.

public class Test {
 public static void main (String [] args ){
  Test obj = new Test();
  int a = obj.calcArea(7, 12);
  System.out.println(a);
 }

 int calcArea(int height, int width) {
  return height * width;
 }
}


Do you know what a static method is?

If not, look it up, but the short answer is that a static method does not (can not) access "this" because it is not assigned to any particular instance of the class. Therefore you can't call an instance method (one that isn't static) from within a static one, because how will the computer know which instance should the method be run on?


If a method is defined as static that means you can call that method over the class name such as:

int a = Test.calcArea(7, 12);

without creating an object,

here; Test is the name of the class, but to do this calcArea() method must be static or you can call a non-static method over an object; you create an object by instantiating a class such as:

Test a = new Test();

here "a" is an object of type Test and

a.calcArea(7,12);

can be called if the method is not defined as static.


your class calcArea should be declared static and if you want to use that class, you have to first create an instance of the class. In the class the parameters of the class should be returned, as someone suggested.

0

精彩评论

暂无评论...
验证码 换一张
取 消