Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ASSIGNMENT (11500317125)

1.Are static members inherited to sub classes?

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.

2.Can we override static methods in java

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.

3. what is the output of this question? Explain

class Test1 {

static int x = 10;

public

static void main(String[] args)

Test1 t1 = new Test1();

Test1 t2 = new Test1();

t1.x = 20;

System.out.print(t1.x + " ");

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.

4. what is the output of this question?Explain

class Test1 {
public

static void main(String[] args)

int x = 20;

System.out.println(x);

static

int x = 10;

System.out.print(x + " ");

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.

5. what is the output of this question? Explain


class Test1 {

int x = 10;

public

static void main(String[] args)

System.out.println(x);

static

System.out.print(x + " ");

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.

You might also like