27/01/25 ---------- Q)What are the similarities between static variables and instance variables? =>Both are data members of a class. =>JVM provides defaut values for both of them. =>Access modifier can be applied for both of them equally Q)Explain about static method in Java? =>If static modifier is applied to an instance method, it becomes a static method. =>A static method can be called on the class. =>A static method can be called on the object also once it is available. Q)What are the rules while implementing a static method? =>In a static method, without creating the object, we should not try to access non static variables and non static methods. =>"this" keyword is not allowed in static method. Q)What is a static initializer? =>static block is known as a static initializer. =>As soon as the class is loaded, static block is executed implicitly. Q)What is the output of the following piece of code? class A{ static int a; static{ System.out.println("a="+a); a=10; }//static block static void m(){ System.out.println("a="+a); } public static void main(String args[]) { System.out.println("static initializer example"); A.m(); } } a=0 static initializer example a=10 Q)What happens in the background as soon as a class(class file) is loaded into memory? 1)memory allocated for static variables. 2)JVM gives default values. 3)JVM executes static blocks 4)JVM calls main method (if any). Q)What is inheritance? =>It is one of the object oriented features. =>Acquiring features from the existing entities is nothing but inhertance. =>Inhertiance is a mechansim of creating new classes from the existing classes using is-a relationship. =>Already existing class is known as a parent class (super class). =>Newly created class is known as a child class(sub class). =>parent classes are generalised classes. Where as child classes are specialised classes. =>Sub classes inherit properties and behaviour from the parent classes. =>Inheritance fecilates code reusability.