Tuesday, 20 September 2011

Java: Object Initialization

    So yesterday we (I) spoke (typed) about overloading, which is, in  a nutshell, having a bunch of methods with the same name take different parameters, so depending on what a user enters for those parameters, the method called differs to accommodate them. Today, we get to talk about initializing objects!

    We know that the correct initialization of variables is a pretty big deal, and this holds true for object initialization, too. Once created, an object needs to be initialized to a known, valid state. Java already makes sure that all an object's instance variables are initialized, but its the programmer's (your) responsibility to make sure the initial state of the object is actually valid with respect to the intended purpose of that object.

    The creation of a new object is a 2-stage process that's triggered by use of the "new" operator, which we've already seen a bunch. 

1. First, memory needs to be allocated for the object (where does it go, otherwise?). This is taken care of by the Java run-time system, though, so not something you need to worry about.

2. Next, the new object needs to be initialized (I'm getting sick of that word, too, don't worry).  This is where all the instance variables of the object are all set to known values, as well as miscellaneous stuff that needs to be taken care for the object to work, such as opening a file.

    During the second step, there're three potential mechanisms for the programmer to use:

1. Direct Instance Variable Initialization
2. Constructors
3. Instance Initializers

And each one gets their own subsection!

Direct Instance Variable Initialization
    Instance variables can be initialized directly, as we're familiar with
Any instance variable declared with an initialization expression is immediately initialized when the new object is created. Simple, but it comes with limitations. Specifically, only a single expression can be used to provide the initial value, and it can't make use of any variables not yet initialized. You can declare an instance variable without it being explicitly initialized, in which care it takes a default value (0 or 0.0 for numerical variables and characters, false for boolean, and for classes, null). As with local variables, explicitly initializing the variable is a good idea, and luckily, direct initialization isn't the only way to do it. An instance variable with no direct initialization can still be explicitly initialized using a constructor.


    A constructor is declared within a class as any other method, but has the specific purpose of initializing the state of a new object. Statements in the method body can assign values to instance variables, create other objects and open files, along with any other operation you might need done. When a constructor terminated, the object should be in a valid state and be ready to use.

    Being a special type of method, a constructor is subject to special rules, the biggest being the constructors name is exactly the name of the class, and doesn't have the preceding return-value attached to it. Example time!

    Note an array list is declared, but not initialized, until we get to the constructor.

    Constructors are pretty cool, they can have actual parameters, so you can control how variables are initialized, and they can be overloaded, which is awesome. In this, they're pretty much methods, without the return value (even though they may as well be void). 
Terminology: A constructor that takes no parameters is called a default constructor.

    Calling a constructor is automatic as part of the "new" operator, and every time you call an object with an empty parameter list, the default constructor is called. This wasn't an issue for us before, despite our lacking constructors, but the Java system will create a default constructor (empty bodied) for us if this is the case, quashing the problem.

Instance Initializers

    There are some cases we can come across where we need a statement sequence to get what we need. We could just put this sequence into a constructor, but this could get annoying if you're overloading them and thus need to repeat the code a bunch of times, so Java gave use this mechanism. An instance initializer associates a block of code directly with a single instance variable. The block is just a compound statement and follows directly after the variable declaration. Here, have another example:

    (Yes, you can call classes within themselves, but that's not important, I'm just lazy).  Look at that. Hell, if you wanted to, you could combine this with instance initialization and have a whole mess of stuff you would probably only find useful in a limited scope. Still, it's there, and therefore good to know about.

That was lengthy. My fingers hurt, so that's it for tonight. Comment, follow, subscribe, share etc, and see you tomorrow!

    And as part of a shameless plug for a friend, if you're interested in classic movies/books/music, visit his site here, and feel free to throw loads of criticism at us.