Special Keywords

In this section, we will discuss several keywords in Java that relate to classes, objects, and methods.

The static keyword

Class variables and class methods are defined with the static keyword. We have written static methods in the past without talking about how they are different from non-static methods. Variables and methods in a class that are not static are instance variables and instance methods, which means there is a different version of each method and variable for each object instance of the class.

Static variables and methods are called class variables and class methods, which means there is only one version of them for all different object instances of the class. Static methods and variables can also be accessed with the class name (as we’ve done before) without having to create an object instance. Regular variables and methods cannot be accessed in this way.

Here’s how to declare a static variable:

visibility static type name;

And here’s how to declare a static method:

visibility static returnType name(args);

Here’s a sample class with static variables and methods:

public class Circle 
{
    public static double pi = 3.14159;

    public static double area(double radius) 
    {
        return pi*radius*radius;
    }
}

Now, because pi and area are static, we access them using the class name (Circle). All of the following are valid:

System.out.println(Circle.pi);      //prints 3.14159
double area = Circle.area(2);       //area = 12.566
Circle c1 = new Circle();
Circle c2 = new Circle();
System.out.println(c1.pi);          //prints 3.14159
double area = c1.area(2);           //area = 12.566
Circle.pi = 3.14;                   //Changes pi for all Circle objects

Static methods cannot refer to any fields. They can only refer to class variables (static variables), method arguments, and local variables.

The final keyword

The keyword final in front of a variable denotes that the variable is a constant. Any variable declared with “final” cannot be modified once it is initialized. For example, the pi variable in the Circle class could be constant – pi should really always be the same value.

Here’s how we would change the class:

public class Circle {
    public static final double PI = 3.14159;

    public static double area(double radius) 
    {
        return PI*radius*radius;
    }
}

Notice that constants are traditionally given names in all capital letters. Also, if I try to change the value of PI, such as:

Circle.PI = 3.14;

I will get a compiler error.

The null keyword

When we declare any variable without assigning it a value, that variable has no value by default. This means if we tried to do something like this:

int val;
val++;

We would get a compiler error, because we can’t add one to a variable with no value. Similarly, if we did this:

Rectangle r;
int a = r.area();

We would also get a compiler error, because we are trying to use r without initializing it to be a new object.

Sometimes you may want to initialize a class type variable, even if you are not ready to create a new object yet. To do this, you can set the variable to the special value null:

Rectangle r = null;

The null value is a valid value for all non-primitive variables (all variables that have a class type). Primitives like ints, chars, doubles, and booleans cannot be set to null.

The this keyword

The keyword this refers to “this object instance”. We can use it inside a class to refer to fields and methods in this class. The keyword is primarily used to distinguish fields from local variables. For example:

public class Person 
{
    private String name;
    private int age;

    public Person(String name, int age) 
    {
        this.name = name;
        this.age = age;
    }
}

Inside the Person constructor, this.name refers to the field called name, while just name refers to the constructor parameter called name. In general, you can say:

this.name

to refer to a method or field (called name) inside THIS class. However, you cannot use the this keyword with static methods or variables, since they don’t depend on a particular object instance.