Docsity

Java Initialization: Understanding Instance Variables, Constructors, and Default Values, Slides of Java Programming

Partial preview of the text

Download Java Initialization: Understanding Instance Variables, Constructors, and Default Values and more Slides Java Programming in PDF only on Docsity!

Initialization

Importance of Initialization

  • Java classes are initialized and have predictable

default values. These values are stored in

memory allocated on the heap when the class

is instantiated

  • Uninitialized data is a source of bugs!

Default Initial Values

  • This table shows the

default initial values for

each of the variable

types.

Type Default Value

boolean False

byte (byte) 0

short (short)

int 0

long 0L

char \u 000

float 0.0f

double 0.0d

object information null

  • If you don't explicitly initialize an instance variable, that variable will retain
its default initial value when new returns its object reference.
// In source packet in file
  • // init/ex1/CoffeeCup.java
// This class has no constructors or initializers
class CoffeeCup {
private int innerCoffee;
As a result, when the reference to a new CoffeeCup object is first returned
by new, the innerCoffee field will be its default initial value. Because
innerCoffee is an int, its default initial value is zero.
  • When you instantiate an object with new, you must specify a constructor. For
example, given the CoffeeCup class above that has two constructors, you could
instantiate it in either of these two ways:
  • // In source packet in file init/ex3/Example3.java class Example3 { public static void main(String[] args) {

// Create an empty cup CoffeeCup cup1 = new CoffeeCup();

// Create a cup with 355 ml of coffee in it CoffeeCup cup2 = new CoffeeCup(355); } // Above requires constructor shown later }

  • Constructors are not methods

Default constructors

  • If you declare a class with no constructors, the compiler will

automatically create a default constructor for the class. A

default constructor takes no parameters.

  • The compiler gives default constructors the same access level

as their class.

  • The compiler would generate the following

two instance initialization methods in the

class file for class CoffeeCup, one for each

constructor in the source file:

// In binary form in file

init/ex8/CoffeeCup.class:

public void (CoffeeCup this) {...}

public void (CoffeeCup this, int amount) {...}

Instance initialization

  • In a constructor, you have the freedom to write as much

code as needed to calculate an initial value. In an instance

variable initializer, you have only an equals sign and one

expression.

// In source packet in file
init/ex9/CoffeeCup.java
class CoffeeCup {
private int innerCoffee;
public CoffeeCup() {
innerCoffee = 355;

Instance Variable Initializers

  • Instance initializers

An instance initializer , also called an instance initialization block is shown here with the CoffeeCup class innerCoffee variable initialized by an instance initializer:

// In source packet in file init/ex19/CoffeeCup.java
class CoffeeCup {
private int innerCoffee;
// The following block is an instance initializer
innerCoffee = 355;
// no constructor here

This manner of initializing innerCoffee yields the same result as the previous two examples: innerCoffee is initialized to 355.

  • Initializers can't make forward references

When you write an initializer (either an

instance variable initializer or instance initializer),

you must be sure not to refer to any instance

variables declared textually after the variable

being initialized. In other words, you can't make a

forward reference from an initializer.

  • Instance data of objects Every object, except class Object itself, has at least one superclass. When an object is created, the Java virtual machine allocates enough space for all the object's instance variables, which include all fields defined in the object's class and in all its superclasses. For example, consider the following classes:
  • // Declared in file Object.java (not In source packet) package java.lang; public class Object { // Has no fields // Has several methods, not shown... }

// In source packet in file init/ex14/Liquid.java class Liquid { // Has two fields: private int mlVolume; private float temperature; // in Celsius // Has several methods, not shown... }

// In source packet in file init/ex14/Coffee.java class Coffee extends Liquid { // Has two fields: private boolean swirling; private boolean clockwise; // Has several methods, not shown... }

  • Order of initialization

In Java, the fields of an object are initialized starting with the

fields declared in the base class and ending with the fields

declared in the object's class. For a CoffeeCup object with the

inheritance path shown in Figure 1, the order of initialization

of fields would be:

  • Object's fields (this will be quick, because there are none)
  • Liquid's fields (mlVolume and temperature)
  • Coffee's fields (swirling and clockwise)
  • This base-class-first order aims to prevent

fields from being used before they are

initialized to their proper (not default) values.