JAVA

Interview Questions on Java - Static & Non static

1. Can we write java program without main?

Yes, by using static block. static blocks will load at the time of class loading.

2. What is the use of static block?

Static block is used to initialize the static variables.

3. How to access the static variables of another class?

By using class name.

4. What is the use of "this" Keyword?

"this" keyword is used to refer the current Object.

5. What is the use of "super" Keyword?

"super" keyword is used to refer the Parent Class Object.

6. Can we use "this" and "super" keywords inside static method?

No. For non-static methods only.

6. Can we use "this" for static variables?

No. You can use class name for static members.

7. public class A { int x= 3; void method1() { int x= 4; // some code } } here, how can you access the global variable inside method1?

By using "this" keyword i.e, "this.x".

8. class A { int x= 3; } class B implements A { int x=1; void method1() { // some code } } here, how can you access the super class variable inside method1?

By using "super" keyword i.e, "super.x".


www.000webhost.com