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 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... }
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); } }
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.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions