[ACCEPTED]-Java only allowing global variables to be static?-global-variables
Your main
method is static
, so it can access only 12 the static
fields of the class directly. Otherwise, you 11 need to create an instance of PlannerMain
first, then 10 you can access its fields. I.e.
public static void main(String[] args){
PlannerMain planner = new PlannerMain();
planner.frame = new JFrame("Land Planner");
planner.makeMap = new JButton("Make Map");
planner.makeMap.addActionListener(new makeMapListener());
...
}
Note that 9 such initialization code is better put in 8 a constructor method.
Btw the variables you refer to 7 are not global. Right now you have as many 6 distinct frame
and makeMap
as many instances of PlannerMain
you 5 create. They would only be "global" (or 4 its closest equivalent in Java) if you declared 3 them public static
- in this case all PlannerMain
instances would 2 share the same frame
and makeMap
, and the external world 1 would see them as well.
There are no global variables in Java in the meaning 45 of variables which would be valid in the 44 whole program.
There are
class variables: These are most 43 similar to what are called "global" variables 42 in other languages. They are declared inside 41 a class with the
static
keyword. There is only 40 one variable for the whole class. (If the 39 class would be loaded again with another 38 classloader, this new class would have new 37 variables, of course.)They should be used 36 prefixed with the class:
MyClass.varName
. Inside of the 35 class you also can omit the prefix, and 34 you also could use them prefixed with an 33 object of that type (but this is discouraged).instance (or 32 object) variables: These are what you have in your 31 example: anything declared inside a class 30 (and outside of any method/constructor/block) without 29 the
static
keyword is a instance variable. For 28 each object of the containing class (which 27 includes objects of any subclasses of this 26 class) there is exactly one variable. (From 25 the "state" view, one could say 24 an object consists of all its instance variables 23 + an identity.)They are used prefixed by 22 an object (of the right type):
myObject.varName
. Inside 21 of non-static methods of this class you 20 can use them unprefixed (this is then referring 19 to the variables of the current object).local variables: These 18 are all variables declared inside of a method 17 or a constructor (or block). They exist 16 once for each invocation of this method, and 15 cease to exist after the method finished. They 14 can only be accessed from inside this method/block, not 13 from methods called from there.
Special cases 12 of these are method/constructor parameters 11 and
catch
-block-parameters.array elements: Every element of 10 an array is a variable of the same type. They 9 can be used everywhere where one has a reference 8 to this array (often in one of the other 7 types of variables), using an index in brackets.
So, in 6 your case you have object variables, and 5 want to use them from a class method (static method). A 4 class method has no current object, thus you have to qualify 3 your variables with an object to use them. Depending 2 on what you want, it may be useful to write 1 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();
}
}
In java the entry point - the main
method must 8 be static, therefore any class variables 7 it accesses must also be static.
To avoid 6 having static member variables spawn out 5 all over the code (which is bad) from this 4 do the following:
public class PlannerMain {
JFrame frame;
JButton makeMap;
public static void main(String[] args){
PlannerMain theApp = new PlannerMain();
theApp.init();
}
private void init() {
frame = new JFrame("Land Planner");
makeMap = new JButton("Make Map");
makeMap.addActionListener(new makeMapListener());
frame.setSize(580,550);
frame.setVisible(true);
}
class makeMapListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
}
}
}
Now your static main
method 3 creates a concrete (non-static) instance 2 of your PlannerMain
class so it can use the member 1 variables.
if you are using some property in a static 8 method then the property should be static. I 7 am guessing you are using these global variables 6 within the main method thus java is throwing 5 this error. If you want to still access 4 a global variable then probably define a 3 class that is initialized with these global 2 variables and instantiate the same in your 1 main function and u can use it.
It is complaining becos the main
method is static
and 3 you trying to access it directly. If you 2 create a an instance of your class PlannerMain
you 1 can access without any problem.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.