1

I recently was pulled back into Java programming for academic purposes and I ran into something interesting during my first project.

I had to design a GUI for a storefront with buttons that did various tasks, and I noticed something weird was happening with my variables. Any CLASS VARIABLES that my buttons changed through their Actions did not need to be declared static, while any CLASS VARIABLES my main method touched had to be declared static.

Why is this? My Main Method and ActionListener Methods are all on the same level of execution. (they're all in the same class, none of them are embedded within each other)

I hope this question is not too obvious/dumb, I am trying to relearn as much about Java as possible.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • The main method is itself static. Action listener methods are not. – Silvio Mayolo Sep 13 '15 at 03:35
  • @SilvioMayolo So is this to say in Java that static methods can only access static (or local) variables? – user3303680 Sep 13 '15 at 03:53
  • A side note, but the term "class variable" is used to refer to static fields. There is no such thing as a non-static class variable. I think you're looking for the word "field" or "attribute" or "property." – Zarwan Sep 13 '15 at 04:03
  • @Zar Good to know. I emphasized the word class because I didn't want it to get confused with a local (method level) variable. – user3303680 Sep 13 '15 at 04:07

1 Answers1

0

Your main method is static, which means it isn't run on an instance of an object, the method is called on the class without having an object of the class instantiated. This makes a certain amount of sense for a main method because it's the first thing your program does so there's no way to instantiate an object.

Static methods and variables exist on the class itself, while instance members are (non-static) variables that are part of each object created from a class. Each object has its own copy of the instance variables declared for that class, giving each object its own state. Since the static variables are on the class, and there is only one copy of the class (it's loaded only once by the classloader), there is only one of each static variable.

I use "class variable" to refer to static variables, and "instance variable" to refer to non-static variables.

In Swing GUI programming there is very little reason to make use of static variables, my advice is to avoid them if at all possible, because having global mutable state is ugly and hard to debug. In Swing all the UI components and listeners are objects, once your main method initializes and displays a main jframe no other static methods or variables are usually needed.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Hmm. So I guess as a rule of thumb I should avoid loading important variables in the main method, that way I won't have to declare them as static? – user3303680 Sep 13 '15 at 04:01
  • @user3303680: in swing programs as i've written them typically the main method only creates a window and displays it. hard to give more advice without seeing code. – Nathan Hughes Sep 13 '15 at 04:02
  • Without going into too much detail I put the file I/O code in the main method, before the window is created. This was to load my fictional store's inventory into array from a textfile. – user3303680 Sep 13 '15 at 04:06
  • @user3303680: that makes sense. you could alternatively pass the inventory that you read in into the jframe subclass object, or have the jframe do the work itself when it is being initialized. but the point is that static means class-level, and non-static members belong to specific instances. – Nathan Hughes Sep 13 '15 at 04:14