Q1. What are static Blocks in Java?
–> Static block in java is simply the initialize block, used for initialization when particular class is loaded in to JVM memory. Static blocks or static initializes are used to initialize static fields in java. we declare static blocks when we want to initialize static fields in our class. Static blocks gets executed exactly once when the class is loaded
Note that, Static blocks are executed even before the constructors are executed.
Example –
package com.java2novice.staticexmp; import java.util.ArrayList; import java.util.List; public class MyStaticBlock { private static List<String> list; static{ //created required instances //create ur in-memory objects here list = new ArrayList<String>(); list.add("one"); list.add("two"); list.add("three"); list.add("four"); } public void testList(){ System.out.println(list); } public static void main(String a[]){ MyStaticBlock msb = new MyStaticBlock(); msb.testList(); } }
Below Output shows that, static block got executed even before constructor executes.
[one, two, three, four]
Q2. What is super keyword in java ?
–>To differentiate between instance variable and local variable, we use this keyword. On the similar line, to differentiate between members of super class and subclass (In case of inheritance), super keyword is used. super is used to refer access method / variable / constructors from immediate parent class (While using Inheritance).
In care of overriding, a subclass object call its own variables and methods. Subclass can not access the variables and methods of superclass because the overriden variables / methods hides the methods and variables of super-class. So to overcome this issue, super keyword is used.
- super is used to refer immediate parent class instance variable.
- super() is used to invoke immediate parent class constructor.
- super is used to invoke immediate parent class method.
Say you are writing a class CHILD extending a class PARENT .
While writing the constructor, if you use super()
the constructor of PARENT will be called.
Or if you want to access a public/protected variable of PARENT in CHILD class, then super.variable
will fetch you the PARENT variable
Example:
Parent class –
package DummyPackage; public class parentClass { //parent class instance variable String name = "Parent Class Name"; //parent class constructor public parentClass() { System.out.println("Parent class constructor is called"); } //method in parent class public void calculate() { System.out.println("Area calculated in ParentClass"); } }
Child / subclass:
package DummyPackage; public class superKeywordInJava extends parentClass { //child class instance variable String name = "Sub class name"; //child class constructor, super() is used here to call parent class constructor public superKeywordInJava() { super(); } //super os used to call methods and variables from parent class public void methodTOCallParentClass() { System.out.println("Name from parent class:: "+super.name); super.calculate(); } public void calculate() { System.out.println("Area calculated in subclass"); } public static void main(String[] args) { superKeywordInJava s = new superKeywordInJava(); System.out.println( "******************************************"); System.out.println("Name is:: "+s.name); s.calculate(); System.out.println( "******************************************"); s.methodTOCallParentClass(); } }
Output:
Parent class constructor is called ****************************************** Name is:: Sub class name Area calculated in subclass ****************************************** Name from parent class:: Parent Class Name Area calculated in ParentClass