Non-static method cannot be referenced from static content

I can not 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) { return height * width; } } 

The following error appears:

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

What does it mean? How can I solve this problem ..?

EDIT:

Based on your advice, I made an instance that 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; } } 

It is right? What difference does it make 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; } } 
+3
source share
6 answers

What Nunn suggested is definitely a problem for your problem. However, I think it would be wise if you got used to it now, while you start learning java early, trying to use static methods as little as possible, unless applicable (for example, utility methods) 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 continue to use java code and, presumably, the code you just wrote, start with objects, such as objects of a polygon of some type, a method like calcArea refers to the instance context, not the static context, so it can work in the internal state of your objects. This will make your code more object oriented and less procedural.

+4
source

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

You can rewrite it as I assume:

 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 follows from the comments, other answers also show that you may not want to go this route for evertyhing: you get only static functions. Find out what static should be in your code, and maybe make yourself an object and call the function from there: D

+7
source

calcArea does not have to be static. To use other methods in the main class, you must create an instance of your own.

 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; } } 
+4
source

Do you know what a static method is?

If not, look, but the short answer is that the static method cannot (cannot) access "this" because it is not assigned to any particular instance of the class. Therefore, you cannot call an instance method (which is not static) from static, because how does the computer know which instance should run this method?

+1
source

If the method is defined as static, this means that you can call this method on the class name, for example:

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

without creating an object,

here; The test is the name of the class, but for this the calcArea () method must be static or you can call a non-static method on the object; you create an object by creating an instance of the class, for example:

 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.

+1
source

your calcArea class must be declared static, and if you want to use this class, you must first instantiate the class. In a class, class parameters should be returned, as someone suggested.

0
source

Source: https://habr.com/ru/post/1336683/


All Articles