This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes nonstatic variables in the main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nonstatic variables in the main method" Watch "nonstatic variables in the main method" New topic
Author

nonstatic variables in the main method

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
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,



thanks!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37902
    
  22
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
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
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!


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Scott A Burch wrote:


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:

Scott A Burch
Ranch Hand

Joined: Jan 04, 2011
Posts: 50
that's what I was looking for. Thanks again!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome. Glad it was helpful!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nonstatic variables in the main method
 
Similar Threads
Var args question...
How do you solve this...??!!
ExamLab: This snippet compiles, but the other one doesn't. Why?
add component to a visible component
Newbie help needed Java Gui can't setText