Data Structures using Java Part 2: Classes, constructors, this, static and lifetime

Defining Classes
—————–
Fields:
1. Variables stored inside objects
2. Used to store data
3. Also known as instance variables

amy.introcude(); // methode call
amy.age;// age is the instance variable

class Human{

	public int age;
	public String name;

	public void introduce(){
	System.out.println("Hi! I am  "+ name + " and I am  "+ age + "years old!");
	}

	public void copy(Human original){
	age = original.age;
	name = original.name;
	}
}

Each Human object can have different values of age and name.

Human amy = new Human();
amy.age = 12;
amy.name = "Amy";
amy.introduce(); // prints out Hi! I am Amy and I am 12 years old!

Human linda = new Human();
amy.copy(linda); // copies content of linda object into amy object

Constructors
————–

class Human{

	public int age;
	public String name;

	public void introduce(){
	System.out.println("Hi! I am  "+ name + " and I am  "+ age + "years old!");
	}

	public void copy(Human original){
	age = original.age;
	name = original.name;
	}

	public Human(String newname){
	age = 12;
	name = newname;
	}

}

The constructor is used to initialised the name of the object to whatever name we pass to it and the age as shown is set to 12 by default. So this code can be converted to:

Human amy = new Human();
amy.age = 12;
amy.name = "Amy";

// this is to be written in place of the last 3 lines of code.. not together
Human amy = new Human("Amy");

Java always provides a default constructor for you. When you creat a new constructor, the default constructor wont be created. If you need onem create one explicity!. The default constructor if written explicitly would look like this:

public Human(){

}

You can also override the default constructor like:

public Human(){
	age = 0;
	name = "Null";
}

The this keyword
——————-

“amy.introduce()” implicitly passes an extra parameter, an object (amy) as a parameter called “this”.

public Human(){
	this.age = 0;
	this.name = "Null";
}

// this method is in the Human class
public void change(int age){

	String name = "Chan";
	this.age;
	this.name = name;
}

The parameters and the local variable always take precedence in methods. Just “age” implies the parameter and “name” the local variable. So we use “this.age”. You cannot change the value of “this”. Complier error if you try to change.

amy.change(10); //Parameters and local variables are obtained. Whatever is in “amy” will be copied onto “this”.

The static keyword
———————

Static field:
1. a single variable shared by a whole class of objects.
2. also called class variables.
3. System.out is a static field.

 class Human{

	public int age;
	public String name;
	public static int number;

	public Human(){
		number++;
	}
}

int kids = Human.number/4;

Static method:
1. Doesnt implicitly take an object as a parameter.
2. In a static method there is no “this”.

 class Human{

	public int age;
	public String name;
	public static int number;

	public Human(){
		number++; // increment by 1 each time an object is created.
	}

	public static void printHumans(){
	System.out.println(number); // number as it is inside the class
	}
}

Life time of variables
———————–
1. A local variable is gone when the method ends.
2. An instance variable, not static, lasts as long as the object lasts.
3. A class variable, static, lasts as long as program is running.

Advertisements
Data Structures using Java Part 2: Classes, constructors, this, static and lifetime