+ 1
What does"static" means in java language?
Ex. Public static void fun(){}
5 odpowiedzi
+ 11
There is a big concept of static keyword in Java
Static keyword is very useful in java if you creat your java veriable and method static it belongs to the class it mens if asses static member in main method So you do not need to create its object you simply asses class static method and veriable using class name
here is syntax :
ClassName.Method() or ClassName.Veriable
here is sololearn explanation
https://www.sololearn.com/learn/Java/2159/
+ 1
Static functions cannot be overridden, while "normal" can.
+ 1
A little example on a static variable,
If you wanna count each time a name is added then you can use the static keyword
https://code.sololearn.com/cZ0N8tScMtgw/#java
+ 1
you can override static method (but only) with other static method
class Test{
public static void main(String[] args) {
var b = new ClassB();
b.doSomething();
} }
class ClassA {
static void doSomething() {
System.out.println("in Class A");
} }
class ClassB extends ClassA {
static void doSomething() {
System.out.println("in Class B");
} }
//output: in Class B
+ 1
//another example. Static variable keeps the same value for all instances of class
class Stat {
static int num;
}
class Main {
public static void main(String[] args) {
Stat.num = 1;
Stat.num++; //num=2
var A = new Stat();
A.num++; //num=3
var B = new Stat();
B.num++; //num=4
System.out.println(B.num); //output: 4
}
}