+ 1
Purpose of static keyword in java
Please help me
3 Réponses
+ 5
Rankush Basically static is use for memory management. Whenever your application start JVM stores all the static things in memory so no need to create object again and again of class. You can call static things without creating the object.
static keyword can be use to make static class, static variable, static methods and static block.
1 - static class
public static class A {
}
2 - static variable
public static final int A = 0;
3 - static method
public static void a() {
}
4 - static block
static {
}
You call directly like for example
public class Example {
public static final int A = 5;
public static void methodA() {
}
}
To call static things just do like this.....
Example.A;
Example.methodA();
if you declare class static, it means all the things inside the class should be static.
static block is use to to initialise the variable, initialise the object. static block execute first before the main method.
+ 1
static means it is a class member, no need to create an instance of class to call that member. if it is a data member then it will be remembered in all instances and if you change it, same value is present everywhere