static keyword
The static keyword is mainly used in java for Memory Management.
- static can be
1. block
2. variable
3. method
4. nested class
- static is not related to any specific object, It is common to the entire class.
- It means that " we can invoke static member without creating an object"
static method:
public static void m1(){
}
- we can call the static method from non static method.
- we can call the static method from another static method
- but, we cannot call non static method from static method. we need to create an object.
static block:
A block of code that has a static keyword
static {
System.out.println("hai");
}
- The static block will be executed before the main method(static block gets executed when the class is loaded in memory)
- static block has more priority than main method
- The class can have more than 1 static block.
- Uses: It is used to initialize a static variable
static variable:
If a variable contains the static keyword, It is called static variable.
static int first = 0;
- static variables are shared among all the objects inside the class.
- class-level variable.
- Only single copy of the static variable is created and shared.
static nested class:
the static class will be created inside another class called a static nested class
public class statickeyword {
static class statickey{
// TODO Auto-generated method stub
}
Comments
Post a Comment