Defining class and creating objects

Defining classes

Adding instance variables

Instance variables can be declared as in the following two examples. Note the public modifier (for now):

1
2
 public int     instanceVar1;
 public String  instanceVar2;

Defining methods

Method definitions have a heading and a method body

Example - Defining a class

1
2
3
4
public class Rectangle {
    public double width;
    public double height;
}

Note that the above class definition merely provides a template or blueprint for the class. No complete program using this class has yet been written, and no object (instance) of this class has yet been created.

Define a class for a Circle that is represented by its radius.

SOLUTION

1
2
3
4
5
6
7
 public class Circle {
     public double radius;
     /*
     * note that int is a wrong choice as radius 
     * CAN be a floating-point value like 1.5 or 2.4
     */
 }

Declaration and instantiation

Declaration

Declaration creates a reference in the memory, which doesn’t refer to any storage space yet.

An object or instance of the new class type is declared in main as follows:

1
2
ClassName  classVar; //declaration
Rectangle r; //example

Instantiation

We perform the instantiation statement to allocate storage space for the instance variables of the object declared and to refer to that memory.

1
2
classVar = new ClassName(); //instantiation
r = new Rectangle(); //example

Combining declaration and instantiation

1
2
3
4
ClassName classVar = new ClassName(); 
//declaration + instantiation

Rectangle r = new Rectangle(); //example

Declare and instantiate an object myCircle of class Circle.

SOLUTION

1
2
3
4
5
 public class Client {
     public static void main(String[] args) {
         Circle myCircle = new Circle();
     }
 }

Although, you can just write the relevant part in written exams:

1
Circle myCircle = new Circle();

Adding method to a class

You can add methods inside the class that can be called on any instance of the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Rectangle {
    public double width;
    public double height;

    public double area() {
		return width * height;
    }

    public boolean isSquare() {
    	if(width == height)
    		return true;
    	else
    		return false;
    	//equally correct: 
    	//return width==height;
    }
}

The dot (.) operator

The dot operator gives us access to the members (instance variables and method) for an object. Think of it as the apostrophe s (’s) of the human language (as in “Gaurav’s class” or “Matt’s workshop”)

1
2
Rectangle r = new Rectangle(); //example
r.width = 5;
1
2
3
4
Rectangle r = new Rectangle(); 
r.width = 5;
r.height = 8;
System.out.println(r.area());

Here, r.area() returns width * height and since the method is called on object r, it returns r.width * r.height. Had the method been called on another object s, it would return s.width * s.height.

Write a piece of code that sits outside the class definition and displays the radius of the object myCircle and also its area.

SOLUTION

1
2
 System.out.println(myCircle.getRadius());
 System.out.println(myCircle.area());

Are there any default values?