Wednesday, 21 September 2011

Java: Objects & References

    We're now familiar with the idea that (should be) variables act as containers that hold representations of values. Haven't looked into how this principle applies to objects, even though we've been able to use them with no problem. While objects represent actual values, they're not stored inside the variable, but rather, the variable holds a reference. The reference acts as a pointer, or an arrow, telling the program where the object is.
I'm an artist!

    Variables of a class type therefore hold as their representation a reference to the object that's stored elsewhere. We give types to variables to indicate what kind of object they can reference, such as "public int x;" can only reference integers.
    A single object with multiple references to it is said to be shared. This can be a good thing, makes a good program if you can utilize it well, but there is a risk that one part of the program will change a certain part of an object such that it fails in respect to another part of the program. This can be minimized by building the class correctly the first time around to make it hard to misuse etc.

    An important issue for reference variables is that the lifetime of the variable is completely separate to the lifetime of the object it references. So like, even if you're out of scope of some variable that's referencing an object, the object itself still exists.

    Just to recap (because I'm more or less confused at this point.), a primitive-type variable (int, float, char etc) holds a representation of some abstract value, but a variable of some class type (String, ArrayList, what have you) holds a reference (pointer) to the actual object which is stored elsewhere.

I think I earned a short one today, 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. 


  1. This seems so difficult.

  2. Question: Is Java a multipurpose language; ie GUI's, buttons, ect - what are it's main functions besides interactive http - or correct my noobness - maybe give some insight into its major functions, and why Java over other languages? Such as Perl, ect.

  3. makes sense, but thanks for writing it out so nicely!