Prev Next

Java / static keyword

Could not find what you were looking for? send us the question and we would be happy to answer your question.

1. What is static keyword in Java?

static is a keyword in Java that can be applied to a member variable (class variable), Java method, nested class, and block.

static keyword cannot be applied on top-level class while will result in compile time error.

2. can the static keyword be applied on outer class?

No. static keyword may be applied for inner class.

3. What is a static variable in Java?

If you declare any variable as static, it becomes static variable or class variable.

The static variable is at class level rather than object level. If Class A has a static int variable counter and A has two instances a1 and a2 both will have a static variable counter whose value would be always same. Both instances share the same copy of static int variable.

A Static variable gets memory allocated only once during the time of class loading. A static variable can be accessed directly by calling ClassName.StaticVariableName without an instance for the class.

4. The advantage of using static variable.

Static variables help writing memory efficient programs at only one copy of the variable being shared across all the class instances.

5. Are static local variables allowed in Java?

No. Unlike C/C++, static local variables are NOT allowed in Java.

6. What is a static block?

A static block, is a block of code inside a Java class that will be executed when a class is first loaded into the JVM. Mostly the static block will be used for initializing the variables.

Static block will be called only one while loading and it cannot have any return type, or any keywords (this or super).

It is also called as Initializer block.

class MyClass
{
	static int val;
	static{
      val = 100;
    }
}
7. Can we have multiple static blocks in a Java class?

Yes, we can have more than one static block in a Java class. It will be executed exactly in the same order it appears.

8. Why can abstract method not be static in Java?

When you declare a static method to be abstract since the static method can be called directly using the class reference, making it abstract would make it possible to call an undefined method which is of no use, hence it is NOT allowed.

9. What is a static method?

A static method belongs to class rather than the object. It can be called directly by using the class name. A static method can access static variables directly and it cannot access non-static variables and can only call a static method directly and it cannot call a non-static method from it.

10. What is nested static class in Java?

Nested static class in Java is a static member of any top-level class. We may create the instance of the nested static class without the instance of enclosing class.

public class StaticNestedClass{

    public static void main(String args[]){
    	StaticNestedClass.NestedStaticClass ns = new StaticNestedClass.NestedStaticClass();
        System.out.println(ns.getDesc());
    }
  
    static class NestedStaticClass{
        public String desc =" Example of Nested Static Class";
      
        public String getDesc(){
            return desc;
        }
    }
} 
11. When nested static class is required?

when we want a single resource to be shared between all instances and normally we do this for utility classes which are required by all components and which itself doesn't have any state.

12. Can a static nested class access instance members of enclosing class?

No. Unlike static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class; it can use them only through an object reference.

13. Difference between static nested class and inner class.

Static nested class can define its own static member while the inner class cannot declare static members.

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. in Static nested class, it can exist even without an instance of enclosing class.

public class StaticNestedClass{
	int nonstaticInt;
	static int staticInt;

    public static void main(String args[]){
    	StaticNestedClass.NestedStaticClass ns = new StaticNestedClass.NestedStaticClass();
        System.out.println(ns.getDesc());
    }
  
    static class NestedStaticClass{
        static public String desc =" Example of Nested Static Class";
      
        public String getDesc(){
        	//System.out.println(nonstaticInt); //Compilation error
        	System.out.println(staticInt);
            return desc;
        }
    }
    
     class NestedNonStaticClass{
        public String desc =" Example of Inner Class";
      
        public String getDesc(){
        	
        	System.out.println(nonstaticInt);
        	System.out.println(staticInt);
            return desc;
        }
    }
} 
14. Explain illegal forward reference error in static block.

Forward reference error is a Java compile time error that occurs when a variable is referenced before it is declared.

15. Order of execution of static block, initializer block and constructor during inheritance.

Static block executes first then the initializer block and constructor.

16. How are static variables treated in clustered environment?

Static variables are always in the scope of the ClassLoader, in most cases per JVM. Each JVM will have its own instance.

«
»
Java Transactions (JTA)

Comments & Discussions