Let's learn java programming language with easy steps

Monday, 4 September 2017

Interview Questions on Static Keyword in Java

Static Keyword in Java Interview Questions and Answers

static keyword in java interview questions

Here we are going to discuss interview questions on static keyword in java. In this article, we will learn interview questions and answers on static keyword in detail.

So without any delay, Let's start static question and answer.

(1) What is static in java?

Java static is a keyword and it is also a non-access modifier. Basically, it is used to memory management so that we can make our memory efficient or save memory. Static means class level in java.


(2) Why we use static in java?

We use static keyword in java so that we can save our memory and make memory efficient. In other words, it is used for memory management.


(3) Where we can use static keyword?

Static keyword can be applied to variables, methods, and nested class only and to define static block also.


(4) What is static variable in java?

When we declared any variable with static keyword is known as static variable. Static variable is a class level variable. When we want to refer common property to all the objects then we use static variable e.g student name and roll no may be different but student's school name will be same for all the students.

Java static variable gets memory only once class area(method area) at the time of class loading.

Syntax: 

public class Demo
{
static String name = "IIT";//static variable
-----
-----
-----
}


(5) What is static method in java?

When we declared any method with the static keyword is known as static method. Static method belongs to the class rather than an object of a class. We can call static method with the class name there is no need to create any instance of a class we can simply access static method preceded by class name.

Syntax:

public class Test
{
static void show()//static method
{
-----
-----
-----
}
psvm
{
Test.show();//calling static method with class name
}
}


(6) What is static block?

We can declare static block with static keyword and opening and closing curly braces. Java static block is executed before the main() method at the class loading time. It is also used to initialize the static data members. Static block belongs to the class level. There are no arguments and return types allowed.

Syntax:

static
{
-----
-----
-----
}


(7) Can we Override static method?

No, We can't override static method in java.

(8) Can we Overload static method?

Yes, We can overload static method.


(9) Why java main method is static?

If main() method is not declared as static then the JVM has to create an object first and then call which causes the problem of having extra memory allocation.

(10) Can we declare multiple static blocks in our code?

Yes, We can declare multiple static blocks and it will execute in the same order it defined.


(11) Can we call super class static method in sub class?

Yes, we can call super class static method in sub class but it can't override in sub class.

(12) Can constructor be static in java?

Constructor cannot be a static because static method belongs to the class, not any object whereas constructor invoked at the time of object creation.

(13) Can we access non-static data member in static method in java?

No, We can't call non-static data member in static member in java.

(14) Can we access static data member in static method?

Yes, We can access.

(15) Can we use this and super in static context?

No, because this and super belongs to objects.

(16) Can we write static public void main(String args[])?

Yes, we can change the order of modifiers in the main method.

(17) Can abstract class have static variable in it?

Yes, We can define static variable in abstract class.


(18) Can we execute java program without main() method?

Share:

1 comment:

  1. Hello There,


    You make learning and reading addictive. All eyes fixed on you. Thank you being such a good and trust worthy guide.


    Hello people, So I was wanting some help returning a variable to a page in tymeleaf. If I could get help just converting one of my methods it would open the door for me and I'd greatly appreciate it. I tried reading the thymeleaf docs but I am having trouble with it.


    Once again thanks for your
    tutorial.



    Regards,
    Morgan

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate