If I make for example one project. Inside with two class. For example: X and Y. I make them what I want, and I want to make a main method in Y. Only system.out.printlf the values in X and Y. But it writes that I need to make them static if I want to run this. I tried to make a new file with only the main class and inside the X Y values but it showed an error. What I missed?
Share
The main method is declared static
public static void main(String[] args) {}Inside of
main, it can only access static variables that exist in the enclosing class. You will see this for example with this bit of code:To make the above work you need to declare
iasstatic:A better way would be to do this:
Static methods can only access static methods and other variables declared as static.
This article may also help you to understand what is going on here.