• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is a workaround to using variables in the main() method without having to declare them static?  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As someone new to Java, I am constantly having to use the main() method and System.out.println() to print out the values of variables in order to make sure what I am doing is correct. However, in order to make any variable accessible to the main method, one has to declare it static. This basically means that I am losing the ability to create instance variables. How can I get around this? I.e. Use the main() method and println() in my classes to continue with my coding experiments but not have to change a variable to static each time I want it printed out?

On another note, why was this limitation put in in the first place? Why not have a main method which could access all variables?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write your logic inside a main method, then.

More details here: Main Is A Pain
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:On another note, why was this limitation put in in the first place? Why not have a main method which could access all variables?

This is, of course, not a random limitation that was added just to make life harder for programmers...

It comes down to what static means.

Member variables and methods that are static work at the class level, rather than the object level. You can call these methods on the class itself. Unlike non-static member variables and methods, they are not associated with a particular instance (= object) of the class. Another way to look at it, is that all objects of a class share the static members - unlike with non-static members, there is only one copy of static member variables, which is shared between all objects of that class.

The main method is a static method, so it's not called by Java on a particular object of the class that contains it, but on the class itself.

See Oracle's Java Tutorials for a more detailed explanation: Understanding Class Members.

Beginners often don't yet understand how to structure programs in a good way, and they often tend to put all their code in the main method, which then quickly grows into a long and complicated list of statements (the page "Main Is A Pain" that Pawel posted about explains this).

To get further than writing everything in your main method, start thinking about how to design your program by dividing it up into classes, where each class and each method has a specific purpose (depending on what your program is supposed to do, of course). Then your main method will most likely in the end be nothing more than kicking off the program, which means: create an object and then call one method on it.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Richardson wrote:. . . On another note, why was this limitation put in in the first place? Why not have a main method which could access all variables?
Don't know; only the people who originally designed the language know exactly, but it is possible to have a good guess.
Historic reasons: in Unix, which the C language was designed for, a quarter‑century before Java® appeared, all programs were automatically started by running a procedure called main, which comes in two flavours:-
void main()...
int main(int argc, char **argv)...

Those of us who write C still use those methods; there are still many people programming C which is a very useful language for OSs and other low‑level stuff. Let's look at the second version. It returns a value, 0 for normal termination, non‑zero for anything else. In theory you are supposed to return different values for different error states and query the returned value for errors. You see vestiges of that if you use NetBeans and it gives you error messages about, “JVM exited with non‑zero exit code,” but 90% of the time people forget to check When Java® came along, there was lots of experience using Exceptions to signal errors, and it was thought that exceptions are better than error codes, so the main method ceased to return error codes, which means its return type has to be replaced by void.
Now, the main method was given public access so it can be “seen” from outside its class and package, and it was given static status so it can be called before any instances of the class are created. That means it has to run when there are no instance variables, so it can't use instance variables, so use of instance variables inside the main method cannot be permitted. The same restriction applies to all static methods. The aruments are to follow in another post: watch this space.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a side note, you can return a status code by calling System.exit.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:. . . int main(int argc, char **argv).... . .
. . . and changed it to public static void main(int argc, char **argv)..., which of course won't compile in Java®. Let's have a look at the arguments.

As you know you can pass command‑line arguments to any instruction. You pass the instruction and any flags and arguments and the runtime can handle the arguments. Let's imagine you have a program compiled from C and you forgot to give it a name, so the compiler default to calling it a.out. You invoke it like this:-
./a.out Campbell is a right nuisance.
The runtime interprets that as an array of Strings, six in all, so it passes 6 to argc (presumably short for arguments count). Note that in Java® you write
java MyProgram Campbell is a right nuisance.
and you get a 5‑element array of Strings. Now, **char is how you declare a String array in C, so that turns into String[]. And Java® arrays all carry their length with them, so you don't need the count. The count disappears, so you are left with
String[] argv where argv presumably is short for arguments value. That was changed to arguments, and for the last ten years or more, it has been customary to write args instead of arguments. So we have it:-
public static void main(String[] args).
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!