Static Keyword
We can use the Static keyword in 5 places.
- Import Statement
- Block
- Class
- Method
- Variable
Static Import Statement
We use import statements to import classes from different packages, adding static to those import statements will only import the static members of the class.
Example:
import static {package name}.{static member name};
import static java.lang.System.out;
Static Block
We create a static block by adding a static keyword before a pair of curly braces.
Example:
static {
staticVariable = 24; // initializing static variable
}
Few points to remember about the static block.
- It is used to initialize the static variables.
- It is executed before the main method, It is executed when the class is loaded in the memory.
- We can have multiple static blocks in a class and these are executed in the same sequence in which they appear in the class definition.
Static Class
There are a few constraints while dealing with static class.
- A class can be made static only if it is a nested class. We cannot declare a top-level class with a static modifier.
- Nested static class doesn’t need a reference of Outer class.
- A static class cannot access non-static members of the Outer class.
Below is an example of a static nested class.
Example:
public class StaticClassExample {
public static void main(String[] args) {
//Static inner class example
DataObject.StaticInnerClass.accessOuterClass();
}
}
class DataObject {
public int nonStaticVariable;
public static int staticVariable; //static variablestatic { staticVariable = 40; //nonStaticVariable = 20; //Not possible to access non-static members } public static int getStaticVariable(){ return staticVariable; } static class StaticInnerClass { public static void accessOuterClass() { System.out.println(DataObject.staticVariable); //static variable of outer class System.out.println(DataObject.getStaticVariable()); //static method of outer class System.out.println(staticVariable); //static variable of outer class System.out.println(getStaticVariable()); // static method of outer class // System.out.println(DataObject.nonStaticVariable); // Not Possible } }
}
Static Methods
Few points to remember about the static methods.
- Static methods can be accessed via its class reference.
- Static methods can call other static methods.
- Static methods also belong to the class-level scope.
- Static methods can access static data members and can change their value.
- They cannot refer to this or super in any way.
Example:
public static int getStaticVariable(){
return staticVariable;
}
Static Variables
Static variables are Class level properties. They do not vary based on the objects of the class.
We can demonstrate this by the below code example, where we have a counter which is static and non-static.
Example:
public class StaticClassExample {
public static void main(String[] args) {
DataObject object1 = new DataObject();
DataObject object2 = new DataObject();
DataObject object3 = new DataObject();
DataObject object4 = new DataObject();
}
}
class DataObject {
public int nonStaticVariable;
public static int staticVariable;public DataObject() { nonStaticVariable++; staticVariable++; System.out.println(staticVariable); System.out.println(nonStaticVariable); }
}
#HappyCoding
Top comments (0)