RaymondBerg.com

This is one of those seemingly simple concepts that never really gets elaborated. Usually a student learning Java for the first time is told. “Write the following:”
public class Person
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

“Don’t ask why, just do it.” Because of that, I’ve heard this question more than a few times in the last couple years.

The main()

The simple definition/purpose of the ‘main()’ method? It’s the starting point for every single Java application you’ll ever write, and this one and only main() makes up the entire life of the program. Once main() starts you’re program is running, and once it exits then your program dies. That’s all it does, really.

I’ll give you some background on the details here, hopefully to reduce the ‘magic’. In the “public static void” part the “public” is the signature that is required as a main() must be accessible to all interested invokers, ‘static’ signifies this method is always run in an identical environment with no worry about conflicting instances (read more on static elsewhere), and ‘void’ designates that there is no return type from the method. The args variable, or whatever you choose to name it, is the array of command line inputs that you specified beyond the name of the Java class. Easy, right?

The Constructor

Constructors are an entirely different beast: an object that is created in a runtime is created by explicit or implicit call to that object’s constructor, essentially establishing the working space for that object. You can have many different constructors, too, but only one can be called for each object that you create. The most common place you see this is in the “new Person()” call where the ‘new’ keyword indicates a new instance of this event, as created in the constructor call ‘Person()’. I know this is getting crazy, but just look at the following example.

public class Galaxy
{
public boolean isSpiral;
public boolean hasLife;
public Galaxy()
{
isSpiral = (Math.random() > 0.5); // 50-50 chance of being spiral
hasLife= (Math.random() > 0.99999999); // tiny chance of supporting life
}
public Galaxy(boolean isSpiralp)
{
isSpiral = isSpiralp; //Specified before creation, guaranteed to be what is requested
}
public Galaxy(boolean isSpiralp, boolean hasLifep)
{
this(isSpiralp); //Call other constructor
hasLife = hasLifep; //set life
}
}
class Universe
{
public static void main(String[] args)
{
Galaxy sagittariusDwarf = new Galaxy(false);
Galaxy milkyWay = new Galaxy(true,true);
Galaxy peacefulProgrammer = new Galaxy();
milkyWay = peacefulProgrammer;
}
}

In this example Galaxy cannot be started by itself, some other program with a main() method (if it’s an application) must actually create the object through the constructor. In this case, we find an example in the Universe class. The Universe ‘starts up’, creates a few galaxies (overwrites some lesser galaxies), and then flickers out and dies. I know it’s sad; pay attention!

The Galaxy object isn’t limited to being used only in the Universe class, but it’s just what we used here. Anything, in theory, could instantiate this galaxy object. I’ve met few girls with Eyes that seemed to instantiate a couple of Galaxy objects, but that’s another blog post.

Self-referential, complicating monkey-wrench.

Some situations call for main and constructor methods, and the constructor could be created inside the runtime. The reason for this is often that the object itself may be created within another program, or it could be something that stands alone. In the case of the universe example, it’s a theology question. If Universe is a class that can be independent of any other class and can suffice by it’s internal definition (a.k.a the programmer is an atheist), then you can just run Universe to create it’s own instance.

public class Universe
{
public static final int TOTAL_ATOMS_POWER_OF_TEN = 81;
public static final boolean IS_STRING_THEORY_LEGITIMATE = false;
public Sphere core;
public void Universe()
{
core = new Sphere(1,1);
for(int i = 0; i < TOTAL_ATOMS_POWER_OF_TEN; i++)
{
core.increaseDensity(10.0);
}
}
public void bang()
{
//code to cause bang
}
public static void main(String args)
{
Universe everything = new Universe();
everything.bang();
}
}

Now suppose the programmer isn't an atheist, then we've got a bit of a problem. We need somebody driving this crazy bus we call life; which will work out totally fine. In fact, not only can we create God, but we can make sure that he's got enough power to create more than one universe. Mix the following class into the Java file for the above and add water:
class God
{
public static final POWER_REQD_PER_UNIVERSE = 42;
private int power;
Vector multiverse;
public God()
{
multiverse = new Vector multiverse;
power = POWER_REQD_PER_UNIVERSE;
}
public void createUniverse()
{
power += POWER_REQD_PER_UNIVERSE;
Universe temp = new Universe();
temp.bang();
multiverse.add(temp);
}
public void getHaircut()
{
//code to get haircut
)
public static void main(String[] args)
{
God me = new God();
me.addUniverse();
me.addUniverse();
me.getHaircut();
}
}

The best thing about designing the original universe to allow for versatile, constructor-based invocation is that we didn't have to change the Universe object to allow for God to create a Universe. In this way we create Universe to be God-agnostic.

I can't believe I wrote an entire blog post just to get to tell that joke.

Posted on March 3rd, 2010 | filed under academia, programming, software, tips | Trackback |

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    follow me on Twitter