+ 3
Can someone explain me what does static do in java?
i know static methods can be overriden? But i just cant to get the clear picture of what static does
8 Réponses
+ 3
Variables:
Static allows the variable to be unique to the entire class instead of each object. It can also be seen by other classes.
Methods:
Static methods can be called anywhere without using an instance of a class and will not be able to access private or public variables of the class as it is again, unique to the class, not instance.
+ 3
In the attached code you'll find a class Animal. In this class you have a few variables and functions and one of them are static.
The static function can be accessed without having to assign it to an instance (or "variable"), this has it's advantages like allowing the user to use a function without using a lot of resources.
In the static void Main() function the static should be there for the program to run from outside of the class.
A very important rule not to forget is that once you have use a static class to accomplish a task, the other function/procedure references should also be static, UNLESS you call an instance or declare a variable.
https://code.sololearn.com/cYIUAWE6PLRE/?ref=app
+ 2
it's a bit clearer now thank you.
+ 1
if a variable is initialized in a class, every object from this class can have a varied value of that variable, but if the variable is declared to be static, all objects become sharing the same unique value of this variable. for example :
class A{
public static int x ;
}
class B {
public static void main(String[] args){
A.x = 1;
A ob1 = new A();
A ob2 = newA();
//ob1.x = 1;
//ob2.x = 2;
}
}
0
static is use to making any method and veriable
object independent
0
or it is use to create
class method and class veriable
that is called through the class name
into the other class
without making object of class