Wednesday, January 18, 2006

Geektalk :: Mr. Happy Object teaches Java static methods

I'm certainly not a bad programmer (though at one time I was horrible) -- my code is clean and readable in any language. That said, it's not always elegant, efficient, or optimal because I don't program as much as some of my peers, and I do so in several different languages. Basically, I am the king of "Hello World!\n".

I'm working on this Luminis User Management system, which is really pretty straightforward. There are a couple tables, views, and stored procedures in the database to do all the heavy lifting, and I'm trying to write the front-end in Java using JDBC to make all the database calls. I took a Java class (no pun intended) in 2002, and I've done a lot of OOP in C++, so I'm expecting Java to behave like C++ (which Steve Taylor called "an octopus made by nailing extra legs onto a dog" in 1998).

I've been enlisting the help of a couple friends whose Java is much better than mine: Phil Feller (someone I know from the Lawson world), and Mike Hickin (who I've known since grade school). In looking at my code yesterday, Mike mentioned to me that I'm using static methods and I don't seem to understand them, but graciously offered to explain it to me if I called him last night. I didn't see his email until this morning, but I figured Google or one of the other many fonts of knowledge on the Internet(s) would happily teach me all about static methods in Java.

And then I met Mr. Happy Object.



Mr. Happy Object, who is clearly a mouth-breather and gets high on the JVM, uses this program to explain everything:


public class MrHappyObject {

private String _mood = _HAPPY;

private final static String _HAPPY = "happy";
private final static String _ANNOYED = "annoyed";
private final static String _ANGRY = "angry";

public void printMood() {
System.out.println( "I am " + _mood );
}

public void receivePinch() {
if( _mood.equals( _HAPPY ) ) {
_mood = _ANNOYED;
} else {
_mood = _ANGRY;
}
}

public void receiveHug() {
if( _mood.equals( _ANGRY ) ) {
_mood = _ANNOYED;
} else {
_mood = _HAPPY;
}
}
}


If the code means nothing to you, the text of the article should still give you a chuckle:

When obj1 and obj2 first instantiate, they have the same state -- they are born happy. As a result, when the printMood() is called on each instance, each object prints "I am happy" to the screen. However, every object instance has its own state that can vary independently of all other instances of that class of object.

As obj1 and obj2 go through their day, their states can vary independently from one another. Here I hug obj1 and pinch obj2:

obj1.receiveHug();
obj2.receivePinch();

obj1.printMood();
obj2.printMood();

Now when I query each instance's mood, obj1 is happy while obj2 is annoyed. While the example is silly, it drives home a point: Every instance possesses its own state, and that state is independent of every other object. Any method whose behavior depends upon the particular state of a particular instance is an instance method. You should not declare such a method as static.


Clearly when the author's mother was pregnant, she overrode the default constructor on him:

smallHuman mySmallHuman = new smallHuman(crackPipe.smoke());


And for those of you non-technical people still reading, that's the part where you laugh.

7 comments:

Annie said...

You are a nerd, and I will never fuck you.

thecardaddy said...

im still trying to figure out how i ended up being friends with such a freak.

the Alpha John said...

Well since you and Jew Drew are in agreement, maybe he can fuck you.

Oh wait, he's gay...

Annie said...

Gays are hawt. Jews are hawter.

So are guys who write in secret code, I just have to maintain my tough exterior. You understand.

thecardaddy said...

im gay? really? i just thought i had some sort of odd fascination with weiners.

Annie said...

You're not alone.

the Alpha John said...

Gross. Why don't you two go make out or something?