Professional Documents
Culture Documents
Assignment
Assignment
ANS ) No. Static members cannot be inherited. However super class and the sub class can have
static method with same signature. Super class static member will be hidden at the sub class.
ANS) No, you can not override static method in Java, though you can declare method with same
signature in sub class. It won't be overridden in exact sense, instead that is called method hiding. But
at same time, you can overload static methods in Java, there is nothing wrong declaring static
methods with same name, but different arguments.
class Test1 {
public
t1.x = 20;
System.out.println(t2.x);
ANS) answer is 20 20
static variable is class level variable. if we do update in any reference then automatically all pointing
reference value are changed.
class Test1 {
public
int x = 20;
System.out.println(x);
static
int x = 10;
ANS) 10 20
Static block is executed before main method. If we declare a Static block in java class it is executed
when class loads.
int x = 10;
public
System.out.println(x);
static
ANS) ERROR
If we are trying to print the instance variable inside the static block or static method without creating
class instance then it will give the error : non-static variable x cannot be referenced from a static
context.