Constructors and Initializing static final


Constructors

Constructors are special methods named after the class and without a return type, and are used to construct objects. Constructors, like methods, can take input parameters. Constructors are used to initialize objects. Abstract classes can have constructors also.

public class Hello{
	// constructor
	public Hello(String wordToPrint){
		printHello(wordToPrint);
	}
 
	public void printHello(String word){
		System.out.println(word);
	}
}
// instantiates the object during creating and prints out the content
// of wordToPrint

It is important to understand that constructors are different from methods in several ways:

  • Constructors can only take the modifiers public, private, and protected, and cannot be declared abstract, final, static, or synchronized.
  • Constructors do not have a return type.
  • Constructors MUST be named the same as the class name. In the Hello example, the Hello object's constructor name is the same as the class name.
  • The this keyword has an additional usage inside constructors. this.method(...) calls a method on the current instance, while this(...) refers to another constructor in the current class with different signatures.

Constructors also can be called through inheritance using the keyword super

public class SuperManClass{
	public SuperManClass(){
		// some implementation
	}
 
	// ... methods
}
 
public class BatmanClass extends SupermanClass{
	public BatmanClass(){
		super();
	}
	//... methods...
}

Initializing static final fields using a static initializer

To initialize a static final fields that require using more than a single expression, a static initializer can be used to assign the value. The following example initializes a unmodifiable set of Strings:

public class CustomWords {
    public static final Set<String> CUSTOM_WORD_SET;
 
    static {
        Set<String> customSet = new HashSet<>();
        customSet.add("Good");
        customSet.add("Morning");
        customSet.add("baz");
        customSet.add("qux");
        customSet.add("99");
        CUSTOM_WORD_SET = Collections.unmodifiableSet(customSet);
    }
}

Object Member vs Static Member

class CustomObjectVsStatic {
    static int customStaticCounter = 0;
    int customMemberCounter = 0;
 
    void increment() {
        customStaticCounter++;
        customMemberCounter++;
    }
}
 
// Usage
final CustomObjectVsStatic obj1 = new CustomObjectVsStatic();
final CustomObjectVsStatic obj2 = new CustomObjectVsStatic();
obj1.increment();
obj2.increment();
obj2.increment();
 
System.out.println("obj1 static counter " + obj1.customStaticCounter);
System.out.println("obj1 member counter " + obj1.customMemberCounter);
System.out.println();
System.out.println("obj2 static counter " + obj2.customStaticCounter);
System.out.println("obj2 member counter " + obj2.customMemberCounter);
System.out.println();
System.out.println("CustomObjectVsStatic.customStaticCounter = " +
        CustomObjectVsStatic.customStaticCounter);
// The following line does not compile. You need an object to access its members
// System.out.println("CustomObjectVsStatic.customStaticCounter = " +
//         CustomObjectVsStatic.customMemberCounter);

produces this output:

obj1 static counter 3
obj1 member counter 1
 
obj2 static counter 3
obj2 member counter 2
 
CustomObjectVsStatic.customStaticCounter = 3

Note : You should not call static members on objects, but on classes. While it does not make a difference for the JVM, human readers will appreciate it.

static members are part of the class and exists only once per class. Non-static members exist on instances, there is an independent copy for each instance. This also means that you need access to an object of that class to access its members.

Basic Programs