What would be the Output
public class Main { static { System.out.println("My Name is Mugil"); } }
OP: My Name is Mugil
followed by exception in Main
public class Main { static { System.out.println("My Name is Mugil"); } public static void main(String args[]) { { System.out.println("What is your Name"); } } }
OP: My Name is Mugil
What is your Name
public class Main { public static void main(String args[]) { { System.out.println("What is your Name"); } } }
OP: What is your Name
4.What is the order they are processed
public class Main { public static void main(String args[]) { { ClassA objA = new ClassA(); System.out.println("What is your Name"); } } } class ClassA { static { System.out.println("I am in ClassA Static"); } ClassA() { System.out.println("I am in ClassA Constrctor"); } { System.out.println("I am in ClassA"); } }
OP:I am in ClassA Static
I am in ClassA
I am in ClassA Constrctor
What is your Name
Static blocks are initiated at first followed by Constructor followed by classA and content in empty class.
5.How to Use Object to invoke function in Class
public class Main2 { public static void main(String args[]) { new A().showName(); } } class A { public void showName() { System.out.println("Hi there"); } }
OP : Hi there
6.How to Find Class of object
public class Main3 { public static void main(String[] args) { ClasB objB = new ClasB(); if(objB instanceof ClasB) { System.out.println(objB.getClass().getName()); } } } class ClasB { }