First meeting with classes

Introduction

Classes are Java main structural units. They are responsive for both holding data (fields) and holding functionality to manipulate this data (methods). Class is a blueprint for creating objects. We can have many objects created for one class. First of all let’s create an example of simple class:

package com.juravine.classes;

public class Circle {

    private int radius;

}

This class represents a circle with some radius. It has a field of type int named “radius”. Our class is not usable yet, so we are going to add some required code lines to it.

Constructor

Constructor is a method named same as class, that is used to perform some logic upon new object creation. Let’s add an empty constructor to a class.

package com.juravine.classes;

public class Circle {

    private int radius;

    //empty constructor
    public Circle() {
    }
}

If no constructors are defined in class it will still have implicit default (empty) constructor. That guarantees that objects of a class can be instantiated.

Class can have any count of constructors. Let’s add another constructor:

package com.juravine.classes;

public class Circle {

    private int radius;

    //empty constructor
    public Circle() {
    }

    //constructor with parameter
    public Circle(int radius) {
        this.radius = radius;
    }
}

Now we have a constructor with a parameter radius. That allows us to create an object of type Circle with some radius. Inside a constructor we initialize class field (this.radius) with parameter value radius. What is this? this keyword is a reference to current object. Currently it is required because we want to use parameter with same name as field, so the compiler should know do we mean field radius or parameter radius. this.radius refers to a field of an object currently created.

Imagine that we have some logic inside empty constructor that we already want to have in some other constructor. Writing it twice would be not very good, you should generally avoid duplication of code if it represents exactly same logic. We are going to use constructor chaining:

package com.juravine.classes;

public class Circle {

    private int radius;

    //empty constructor
    public Circle() {
        System.out.println("Constructor is called");
    }

    //constructor with parameter
    public Circle(int radius) {
        this();
        this.radius = radius;
    }
}

Empty constructor prints a message that we also want to get printed when calling to constructor with parameter. Inside constructor with parameter we should add line this(); that means “call current object constructor with args specified in parenthesis (in our case with no args)”. Calling to another constructor can be only the first line inside other constructor or your code will not compile.

Getters and setters

Now we need to have some access to our field radius.

package com.juravine.classes;

public class Circle {

    private int radius;

    //empty constructor
    public Circle() {
        System.out.println("Constructor is called");
    }

    //constructor with parameter
    public Circle(int radius) {
        this();
        this.radius = radius;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
}

Getter is a method that allows to access object fields from outside the class (as you can see field radius is private, that means that without getter we can’t access from anywhere except the class Circle). Setter is required to init field “radius” with some value or update it.

Usage

Ok, we have a complete class, let’s create some objects. Create a class ObjectCreation in same package.

package com.juravine.javabasics.classes;

public class ObjectCreation {

    public static void main(String... args) {
        Circle circleOne = new Circle();
        circleOne.setRadius(2);
        Circle circleTwo = new Circle(3);
        System.out.println("Circle one radius " + circleOne.getRadius());
        System.out.println("Circle two radius " + circleTwo.getRadius());
    }
}

First we create object “circleOne” calling to the no-arguments constructor. Then we set its radius value to “2”. After that we create object “circleTwo” calling to constructor with “radius” parameter set to 3. In the end call to System.out should return the following lines:

Circle one radius 2
Circle two radius 3
Scroll to Top