Prev Next

Java / Class Variables (Static Variables)

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

How do I define a constant?
Posted on Apr 1, 2016 by Senthil Kumar.

The variable should be declared with static and final modifiers. static ensures that only one variable exists for all instances of the class and final makes its value not changeable once initialized.

static final int MAX_HEIGHT = 50;

What are static variables?

Static variables are class level variable where all instances of the class refer same variable. When an instance update its value, all the other objects will see the new value.

Explain static keyword in Java.

The static keyword can be applied to,

  • static variables,
  • static methods,
  • static block,
  • static inner class,
  • and interface static method (introduced in Java 8).
What is a static class in Java?

A Class can be made static only if it is a nested class (class within a class). The nested static class can be accessed without having an object of outer class.

It can access static data members of outer class including private. Static nested class cannot access non-static data member or method.

If you have the static member inside static nested class, it can be accessed directly and you don't have to create instance of static nested class.

The static class are also known as static nested class or static inner class .

class OuterClassEx{  
  static int val=30;  
  static class InnerClassEx{  
   void msg(){System.out.println("Value is "+val);}  
  }  
  public static void main(String args[]){  
  OuterClassEx.InnerClassEx obj=new OuterClassEx.InnerClassEx();  
  obj.msg();  
  }  
}  
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 also not have 'this' or 'super' keyword.

public class TestStaticBlock {
	static int val;
	static {
		val = 100;
		System.out.println("Hello from static block. val ="+ val);
	}
}
Can we have multiple static blocks in a Java Class?

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

Can Interface in Java have static methods?

Yes, since Java 8, interfaces can have static methods.

Can we overload static methods in java?

Yes, we can overload static methods in java.

Can we override static methods in java?

No, we can not override static methods in java.

Can we invoke a superclass static method from the subclass?

Yes. We can invoke it using the superclass name or subclass name.

Can we serialize static variables?

No,Static variables are shared variables and don't correspond to a specific object.

What is a static import?

Static imports allow us to import all static fields and methods of the class, you can access them without class name reference.

static import is introduced in Java 5.

import static java.lang.System.out;
import static java.lang.Math.*;

class WithStaticImports {
  public static void main(String [] args) {
    out.println("round " + round(1032.897));
    out.println("min " + min(60,102));
  }
}
Can we execute a program without main () method?

Yes, it is possible in previous versions of Java, not since JDK 1.7 using static block.

«
»
Package

Comments & Discussions