Java only allowing global variables to be static?

后端 未结 5 508
难免孤独
难免孤独 2021-01-19 01:40

So I just started coding a Java program I\'m writing and it\'s telling me that my global variables need to be static. I don\'t understand why it\'s telling me this because I

5条回答
  •  无人及你
    2021-01-19 02:28

    There are no global variables in Java in the meaning of variables which would be valid in the whole program.

    There are

    • class variables: These are most similar to what are called "global" variables in other languages. They are declared inside a class with the static keyword. There is only one variable for the whole class. (If the class would be loaded again with another classloader, this new class would have new variables, of course.)

      They should be used prefixed with the class: MyClass.varName. Inside of the class you also can omit the prefix, and you also could use them prefixed with an object of that type (but this is discouraged).

    • instance (or object) variables: These are what you have in your example: anything declared inside a class (and outside of any method/constructor/block) without the static keyword is a instance variable. For each object of the containing class (which includes objects of any subclasses of this class) there is exactly one variable. (From the "state" view, one could say an object consists of all its instance variables + an identity.)

      They are used prefixed by an object (of the right type): myObject.varName. Inside of non-static methods of this class you can use them unprefixed (this is then referring to the variables of the current object).

    • local variables: These are all variables declared inside of a method or a constructor (or block). They exist once for each invocation of this method, and cease to exist after the method finished. They can only be accessed from inside this method/block, not from methods called from there.

      Special cases of these are method/constructor parameters and catch-block-parameters.

    • array elements: Every element of an array is a variable of the same type. They can be used everywhere where one has a reference to this array (often in one of the other types of variables), using an index in brackets.

    So, in your case you have object variables, and want to use them from a class method (static method). A class method has no current object, thus you have to qualify your variables with an object to use them. Depending on what you want, it may be useful to write it this way:

    import java.awt.event.*;
    import javax.swing.*;
     
    public class PlannerMain {
       JFrame frame;
       JButton makeMap;
       
       void initGUI() {
         frame = new JFrame("Land Planner");
         makeMap = new JButton("Make Map");
         makeMap.addActionListener(new makeMapListener());
         frame.setSize(580,550);
         frame.setVisible(true);
       }
    
       public static void main(String[] args){
         PlannerMain pm = new PlannerMain();
         pm.initGUI();
       }
    }
    

提交回复
热议问题