public static void whaaaat?

BCPL ‘Hello World’ program

This post is inspired in part based on a question (Facebook account and membership to the group required) that came up on the AP Computer Science A Teachers Facebook group (please consider joining if you teach Java or AP CS A). For those without Facebook access, the question amounts to “What does each part of public static void main(String[] args actually do?”.

Without going into the debate of objects first (I am firmly in the procedural programming first, object oriented programming second camp – a blog post to come soon), let us see if we can take deep dive into public static void main(String[] args). I always like to begin with a bit of historical context.

In the beginning of our story (around 1967), there was BCPL – “Basic Combined Programming Language”. BCPL bears little resemblance to our modern Java but would go on to inspire Ken Thompson and Dennis Ritchie to write a stripped down version of BCPL which, quite understandably, they called B. B seems to be likely where we first see the term main. B then begets C, C inspires C++ (C with objects!) and C++ inspires Java (C++, but it is harder to shoot off your entire leg!). As such, many languages to this day have a convention of starting code execution with main.

Now, what is main? Simply put it is a place to start code. Other programming languages, famously Python and JavaScript, merely assume the first line of code in the file is where to start execution, hence the lack of required main in either of those languages. Even in 2020, main is the place where Java starts executing code. Why? Because Ken Thompson and Dennis Ritchie decided it would be that way in B, then C, C++ followed C and Java followed C and C++.

So now we know why main (kind of anticlimactic, isn’t it?), but why does main have to be public? This brings us into the definition of public versus private. public items are accessible from outside a class, private items are accessible only from within a class. However, there is no class to begin with when we start (again, starting by convention with main), so making main private would mean that we cannot start our program. Java cannot access our main because it is private, so we have to make main public. Note this deftly avoids the definition of class, but it isn’t too hard to spend a little time showing a black box and explaining information inside the box is private and information outside of the box is public. A class is the code written definition of that box and an object is that box actually in action (i.e. executing).

This also explains a bit why main has to be static. A rough understanding, especially for raw beginners, of static is that anything static exists outside and independent of a class in memory. Again we run into a problem if main is not static. If main cannot exist outside of a class and we agree that we have to start with main outside of a class, then starting our Java program will be rather difficult without making main static. For more advanced students, this can lead into interesting conversations about the new keyword and a better understand of the keyword static.

Now we have to talk about String args[]. It isn’t too hard to explain what a String is especially if the students have a background in something like Python. As to arrays of command line arguments, one of many advantages of teaching Java on the command line interface is it makes explaining args relatively easy with code like this:

public class Args
  public static void main(String args[])
    System.out.println("Number of args: " + args.length);
    for (String arg : args)

Producing a run of:

For the majority of teacher’s who use a graphical IDE, maybe even use this blog post to demonstrate how args works.

Of course, this opens up having to explain the foreach loop. But such is the nature of public static void main(String[] args). Any attempt to explain the details does lead into having to either explain many other things or do what I call hand waving. Namely, just trust me and you will understand later. No part of me will judge a teacher who tells a student just trust me and you will understand later on any of this, but I hope this post can inspire even a small amount of decreased hand waving.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: