I'm trying to put an interface on some code that only ran using Console. I have a snag with using nonstatic variables in the main method. I know they do not belong there, but I am stumped as to how to get them out. Specifically,
I have a snag with using nonstatic variables in the main method.
Since main method is static, you can't use nonstatic variables in it. I will suppose you already know why is that so.
Your code doesn't really tell much because it's not even compilable. At line 10 you have a constructor named SameEventTimer2, though your class is named A. Again at line 23 you are trying to instantiate that class, but it's not defined (or at least you didn't post the code of it). And at line 26... well, I don't really get what you're trying to do there.
If you want to get further help, I think you should post a better example of your problem, or at least try to TellTheDetails of it.
The quieter you are, the more you are able to hear.
Why are you trying to use any variables in your main method? The idea of a main method is to start your application, not to run the application. Its ideal length is one statement, so a typical example would beNo need to use any variables at all. Those will all live in the MyApplication class. Obviously it is sometimes impossible to get your main method that short.
Scott A Burch
Joined: Jan 04, 2011
Let me try to explain this better. Got rushed on posting yesterday and messed it up - sorry for the confusion. I am posting abstracted code that goes to the heart of the question; there is too much other code to post have it compile. The code was originally written as a console application, that is, no GUI. I could put the integer values directly into the main() method, compile it, and it would run. What I am trying to do is give it a GUI, specifically, the integer values in the main() method are supplied by three JSpinners. I am looking for suggestions as to how I can express the main() method without running into the nonstatic variable problem. I know the integer arguments cannot live in the main() method, I just don't know of an alternative. Suggestions? (Besides give up?) Thanks!
Class A has a non-static member variable called b. When a new A is created, before any constructor is invoked, it will set its b value from hSpinner.getValue(). I have no idea wher hSpinner is supposed to be coming from.
Here you're declaring a constructor that accepts 3 ints, but completely ignores them. What's the point supposed to be?
Here you're invoking that constructor, passing it arguments that won't do anything anyway. You're trying to pass it b, c, and d, but those don't exist because main is static, so there's no "A" object, no "this".
Also, even if you were to pass them, and even if you were to use them, something is pointless. You would presumably use them to set the b, c, and d of the newly created "A" object, but those are already set from hSpinner, etc., before any constructor is invoked. You seem to be trying to initialize member variables to values they already have anyway, using those very same variables, but in a context where they don't exist.
So do you want to pass these values to a constructor? If so, it's like this: