Creating an object

To create an object, a Java class is instantiated by using the new operator. So, if we want to create an object of the NotThatSimple class, we would have to write:

NotThatSimple notThatSimpleObject = new NotThatSimple( 42 );

 

This statement creates a new NotThatSimple object and performs the following three actions:

 

1. declaration,

2. instantiation (object creation), and

3. initialization.

Declaration


The declaration part could have been written separately by writing:

NotThatSimple notThatSimpleObject;

 

This statement only creates a variable named notThatSimpleObject to hold an object of type NotThatSimple, but does not instantiate it. As it is common use in Java (and the Style Guide fanatic I am), I will use names that begin with an uppercase letter for class names and a lowercase letter for object names.

Instantiation


The object is instantiated by the new operator, followed by a class constructor call. The instantiation part is the following statement:

new NotThatSimple( 42 );

 

The new operator creates an object and returns a reference to that object:

notThatSimpleObject = new NotThatSimple( 42 );

 

Note that before you can write the object instantiation in this form, you must have declared notThatSimpleObject to be of type NotThatSimple earlier. Otherwise, you'll get an exception (error), every Java programmer is familiar with: the NullPointerException.

Initialization


The initialization of an object is done by the class constructor call after the new operator:

NotThatSimple( 42 );

 

Each class has at least one constructor, which can easily be distinguished from a method, because it has the same name as the class and has no return type. Our NotThatSimple class has only one constructor that takes one integer as an argument (the number "42" in the example).

[see CW96, "Creating Objects"]

 

Now we have got our object notThatSimpleObject we can work with.

 

As mentioned in the previous chapter, an object has state, behaviour, and identity.

The state of our object is determined by the current value of the variable value. To make clear that a variable belongs to an object (instance of a class), it is explicitely called instance variable.

The object's behaviour is determined by the methods it (or any of its superclasses) can perform. Our object has only one method getValue(), which may be used to get the object's current state.

And finally, our object also has an identity, determined by the object's name.

 

As we heard before, a class serves as a "blueprint" for similar objects.

This means, we are free to create as many objects of the NotThatSimple type as we want. For example, the statement

NotThatSimple similarObject = new NotThatSimple( 27 );

 

creates another object that is similar to the notThatSimpleObject. It has the same behaviour, because it is also of the NotThatSimple type, but its state and identity are different.

 

Now, if we want to change the state of an object, we have to call its methods.

Neural Net Components in an Object Oriented Class Structure