0
Can someone explain to me what is Static? Especially when we put static on the Variable. What will happen to the Variable?
Someone explain it to me please 😥😅 I kind of dont get it.
6 Antworten
+ 4
In the Java programming language, the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type.
This means that only one instance of that static member is created which is shared across all instances of the class.
The keyword can be applied to variables, methods, blocks and nested class.
Java Static Variables:
A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.
Few Important Points:
- Static variables are also known as Class
Variables.
- Unlike non-static variables, such
variables can be accessed directly in
static and non-static methods.
+ 4
Java static variable
We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class.
Since static variables are shared across all the instances of Object, they are not thread safe.
Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it with ClassName.variableName
// static variable example
private static int count;
public static String str;
public static final String SL_USER = "myuser";
+ 2
Hello, 😊
Please, Use the search feature before posting as to reduce the number of duplicate threads in Q/A section!👍
https://www.sololearn.com/post/10362/?ref=app
Please, read our guidelines:
https://www.sololearn.com/discuss/1316935/?ref=app
An useful code for any new user here!;)
https://code.sololearn.com/WvG0MJq2dQ6y/
+ 2
Miyuna☕
I do not know how old you are, how to adjust the answer to your question?
I hope, this explanation will help!👍
https://code.sololearn.com/W2LnAYam75y4/?ref=app
+ 1
Static means accesible to its class objects
https://www.sololearn.com/learn/Java/2159/