11. What is static variable?
static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.
static variable gets memory only once in class area at the time of class loading.

12. What is static method?
A static method belongs to the class rather than object of a class.
A static method can be invoked without the need for creating an instance of a class.
static method can access static data member and can change the value of it.

13. Why main method is static?
because object is not required to call static method if It were non-static method, jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

14. What is Singleton class?
Singleton is a class which has only one instance in whole application and provides a getInstance() method to access the singleton instance. There are many classes in JDK which is implemented using Singleton pattern like java.lang.Runtime which provides getRuntime() method to get access of it and used to get free memory and total memory in Java.
How to create Singleton will discuss in next chapter.

15. What is static block?
Is used to initialize the static data member.
It is excuted before main method at the time of classloading.

Example:
class Book{
     static String msg= “Welcome to KP blogs”;
     static{
          System.out.println(“Static Block: “+msg);
     }

      public static void main (String[] arg){
            System.out.println(“Main Method: “+msg);
       }
}

After run output will be:
Static Block: Welcome to KP blogs
Main Method: Welcome to KP blogs

16. Can we execute a program without main() method?
Yes, using static block we can execute. But JDK version should be less then 1.7 not equal or high.

17. Difference between static method and instance method?

               Static method                 Instance method
A method declared as static is known as static method. A method not declared as static is known as instance method.
Object is not required to call static method. Object is required to call instance methods.
Non-static members cannot be accessed in static context (static method, static block and static nested class) directly. static and non-static variables both can be accessed in instance methods.
Example: public static int cube(int n){

return n*n*n;

}

For example: public void msg(){…}.

18. Difference between this and supper keywords?

               super                 this
Super keyword in java is a reference variable that is used to refer parent class object this keyword in java is also a reference variable that is used to refer current class object
super is used to access methods of the base class  this is used to access methods of the current class.
Extending the notion, if you write super(), it refers to constructor of the base class and if you write this(), it refers to the constructor of the very class where you are writing this code.

 

19. Can we create Abstract class object? If not then why we can not create the Abstract class object?

An abstract type is defined largely as one that can’t be created. You can create subtypes of it, but not of that type itself. An abstract class has a protected constructor (by default) allowing derived types to initialize it. In the case of Interface also we cannot create object because Interface don’t have constructor.

 

20. Singleton design pattern vs Singleton beans in Spring container

A singleton bean in Spring and the singleton pattern are quite different. Singleton pattern says that one and only one instance of a particular class will ever be created per classloader.

The scope of a Spring singleton is described as “per container per bean”. It is the scope of bean definition to a single object instance per Spring IoC container. The default scope in Spring is Singleton.

Even though the default scope is singleton, you can change the scope of bean by specifying the scope attribute of <bean ../> element.

<bean id=”..” class=”..” scope=”prototype” />

 

912 total views, 1 views today