Head First Java Chapter 09 summary

Udesh Kavinda
4 min readNov 17, 2021

Constructors

No argument Constructors

As the name specifies the no argument constructors of Java does not accept any parameters instead, using these constructors the instance variables of a method will be initialized with fixed values for all objects.

Public class MyClass {
Int num;
MyClass() {
num = 100;
}
}

You would call constructor to initialize objects as follows

public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.num + " " + t2.num);
}
}

Parameterized Constructors

Most often, you will need a constructor that accepts one or more parameters. Parameters are added to a constructor in the same way that they are added to a method, just declare them inside the parentheses after the constructor’s name.

Here is a simple example that uses a constructor −

// A simple constructor.
class MyClass {
int x;

// Following is the constructor
MyClass(int i ) {
x = i;
}
}

You would call constructor to initialize objects as follows −

public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
}
}
  • Constructors can be public, private,or default.
  • Marking the Constructor as private doesn’t mean nobody can access it, it just means that nobody outside the class can access it.
  • All the constructors in an object’s inheritance tree must run when you make a new object.
  • Even Abstract class have constructors, Although you can never say new on an abstract class, an abstract class is still a super class, so its constructors runs when someone makes an instance of concrete subclass.
  • The only way to call a super constructor is by calling super(), this calls the super constructor.
  • Compiler puts a default call to super() if we don’t. and it is always the no-args super which is inserted.
  • The call to super() must be the first statement in each constructors.
  • Use this() to call a constructor from another overloaded constructor in the same class.
  • The Call to this() can be used only in a constructor, and must be the first statement in a constructor.
  • A constructor can have a call to super() or this() but never both.

Object Life and Scope

  • An objects life depends on entirely on the life of references referring to it.
  • A local variable lives only within the method that declared the variables.

public void read(){

int s = 42;

// 's' can be used only within this method.

}

  • An instance variable lives as long as the object does. If the object is still alive, so are its instance variables.

public class life{

int size;

public setSize(int s){

size = s;

//'s' disappears at the end of the method,

//but 'size' can be used anywhere in the class.}}

  • life A local variable is alive as long as its Stack frame is on the stack.
  • Scope A local variable is in scope only within the method in which the variable was declared.

Garbage Collection

  • An object is alive as long as there are live references to it. If a reference variable goes out of scope but is still alive.
  • Object are killed in these three ways
  1. Reference goes out of scope permanently.
  2. Assign the reference object to another object.
  3. Explicitly set the reference to null.

BULLET POINTS

  • Java has tow areas or memory we care about,
  1. The Stack
  2. The Heap
  • Instance Variables are variables declared inside a class but outside any method.
  • Local variables are variables declared inside a method or method parameters.
  • All local variables live on the stack, in the frame corresponding to the method where there variables are declared.
  • Object reference variables work just like primitive variables — if the reference is declared as a local variables, it goes on the stack.
  • All object live in the heap, regardless of whether the reference is a local or instance variable.
  • Instance variables live within the object they belong to, on the heap.
  • If the instance variables is a reference to an object, both the reference and the object it refers to are on the HEAP.
  • A constructor is the code that runs when you say new on a class type.
  • A constructor must have the same name as the class, and must not have a return type.
  • You can use a constructor to initialize the state of the object being constructed.
  • If you don’t put constructor in your class, the compiler will put in a default constructor.
  • The default constructor is always a no-args constructor.
  • If you put a constructor-any constructor-in your class, the compiler will not build the default constructor.
  • If you want a no-arg constructor, and you’ve already put in a constructor with arguments, you’ll have to build the no-args constructor yourself.
  • Always provide a no-args if you can, to make it easy for programmers to make a working object, supply default values.
  • Overloaded constructors means you have more than one constructor in your class.
  • Overloaded constructors must have different argument lists.
  • You cannot have two constructors with the same argument lists. An argument list includes the order and/or type of arguments.
  • Instance variables are assigned a default value, even when you’ve not explicitly assign one.

--

--