Friday, May 18, 2012

The 'static' keyword


Download the code


Static variables & methods are associated with the class rather than an instance of an object of that class.

Usually, the data contained within an object, belongs only to that instance.

If you want all instances of a class to share some data, use a static variable. A static variable belongs to the class, rather than an instance the class (object).

The example below uses a static variable to keep count of the number of Widget objects that have been created.

The Widget class contains a single static variable called _numberOfWidgets. No matter how many instances of Widget are created, there will be a single value associated with the _numberOfWidgets variable.

A static method is provided to set the value of _numberOfWidgets and a static method is provided to get the value of _numberOfWidgets. In this example, the constructor calls the set method and in turn, the set method increments the value.


In the TestWidgetClass below,  a number of Widget instances are created. Note that the call to the get_numberOfWidgets method is made by referencing the class directly, rather than by instance of the class.



And the output is:




"Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/KmbYd7 )

In a UML class diagram, Static variables & methods are represented with an underline.



Here's a cat to keep you calm:


Parameters / Arguments



See also:
"Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)" ( http://bit.ly/XxwPUf )

JIT

JIT = Just In Time.

Here's a JIT cat;


Thursday, May 10, 2012

Multiplicity

Multiplicity refers to the number of relationships that exists between 2 classes in a composite relationship.

In the diagram below, Player has a 1 to 1 relationship with the Sword class and the Flight class.

Composition: Has-a



"Object composition - Wikipedia, the free encyclopedia" ( http://bit.ly/K0qMRm )

"Has-a - Wikipedia, the free encyclopedia" ( http://bit.ly/JWe9mb )

Composition is a relationship between objects. When one object is composed of others, this is said to be a composite relationship.

In the class diagram below, the Player class has-a relationship with the InventoryItem class. Another was to say this is that the Player class is composed of multiple classes.



Object-oriented design & programming

"Object-oriented programming - Wikipedia, the free encyclopedia" ( http://bit.ly/cLXHSq )

"Object-oriented design - Wikipedia, the free encyclopedia" ( http://bit.ly/cbnJAY )

Are you reading the javapants blog?

Hi. This blog is not meant to be read in a linear fashion. Refer to it by using the tag list down the right hand side. Get what you need. Clear as mud?