Java: Static

This entry is part 32 of 54 in the series Learn Java

The static keyword is used to create fields and methods that can be accessed without having to make an instance of the class.

Static (class) members only exist in one copy, which belongs to the class itself, whereas instance (non-static) members are created as new copies for each new object.

This means that static methods cannot use instance members since these methods are not part of an instance.

On the other hand, instance methods can use both static and instance members.

package javaapplication25;

class MyCircle {

    float r = 5;            // instance field
    static float pi = 3.14F; // static/class field

    // Instance method
    float getArea() {
        return newArea(r);
    }

    // Static/class method
    static float newArea(float a) {
        return pi * a * a;
    }
    
    public static void main(String[] args) {
        
        MyCircle c = new MyCircle();
        
        float x = c.getArea();
        
        System.out.println(x);
           
    }
}

To access the getArea method, we create an instance c for MyCircle class and access the method using a dot as in c.getArea().

static

Java: Garbage collector

This entry is part 31 of 54 in the series Learn Java

The Java runtime environment has a garbage collector that periodically releases the memory used by objects when they are no longer needed.

This frees the programmer from the often tedious and error-prone task of memory management.

An object will be eligible for destruction when there are no more references to it. This occurs, for example, when the object goes out of scope.

An object can also be explicitly dropped by setting its references to null.

package javaapplication24;

public class Garbage {

    public static void main(String[] args) {

        Garbage g = new Garbage();

        g.printOut();

        g = null;
    }

    public void printOut() {

        int x = 20;

        System.out.println(x);
    }
}

garbage

Java: Default values

This entry is part 30 of 54 in the series Learn Java

The default value of an object is null. For primitive data types the default values are as follows: numerical types become 0, a char has the Unicode character for zero (\0000) and a boolean is false.

Default values will be automatically assigned by the compiler, but only for fields and not for local variables.

package javaapplication23;

public class PrintInt {

    int x;  // field is assigned default value 0

    public static void main(String[] args) {

        int y = printInt();

        System.out.println(y);

        PrintInt p = new PrintInt();
        System.out.println(p.x);
    }

    static int printInt() {

        int x; // local variable must be assigned if used

        x = 10;
        return x;
    }
}

default

Java: Null object

This entry is part 29 of 54 in the series Learn Java

The built-in constant null is used to represent an uninitialized object.

It can only be assigned to objects and not to variables of primitive types.

The equal to operator (==) can be used to test whether an object is null.

package javaapplication22;

public class PrintString {

    public static void main(String[] args) {

        String s = null;
        
        if (s == null) { 
            s = new String();
            s = "Hi There!";
            
            System.out.println(s);
        }
    }
    
}

null

 

Java: Default constructor

This entry is part 28 of 54 in the series Learn Java

It is possible to create a class even if no constructors are defined.

This is because the compiler will then automatically create a default parameterless constructor.

package javaapplication21;

public class Print {

    public static void main(String[] args) {

        Print print = new Print();

        print.printOut();
    }

    public void printOut() {
        System.out.println("Hello");
    }
}

defaultconstructor