+ 1
what is the meaning and use of 'static'
when defining a method we use public or static so what is the correct meaning of using it there
3 ответов
+ 2
It makes the variable/function part of the class, not an object. This means you don't need a new object to access them, and every object of that class shares the very same functions and variables, not there own versions of them.
Example: You have a static function 'static int fac(int a);' in a class called 'Math'. Now you can call it in other classes using Math.fac(a); instead of needing an object Math math = new Math(); and calling math.fac(a);
This can also be used to call functions directly in the main function.
+ 1
public mean your method can be accesible outside the class.
static mean you dont need to instatiate the class to call the method
+ 1
static method or atribute no need to access with a object. You access them directly with class name.