This question already has answers here:
When are static variables initialized?
(7 answers)
Closed 7 years ago.
In Java I've always seen Class variables (defined with keyword static) as equivalent of global variables in other languages such as C, defined inside a class to avoid name conflicts. In C you can refer a global variable from any function at any time it always exists while the program is running.
What about static variables in Java. Do they always exist? Do they always get loaded when they're referred? I wanna know if it's always safe when I use a static variable from a static method of another class.
Also does a static variable ever get destroyed?
A static variable is initialized when the class is initialized, and so will always be valid (Initialization of Classes and Interfaces).
Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables) declared in the class.
If the value of the static field is changed, and there are no other references to the previous value, then the previous value will be garbage collected. However, the reference itself will always exist, so if "safe" means "never access illegal memory", it is always safe (when using the Java language in general, not just in this case).
Related
This question already has answers here:
What does the 'static' keyword do in a class?
(22 answers)
Difference between Static and final?
(11 answers)
Closed 2 years ago.
I am confused about when to use Static variables/methods and exactly what they do. I know Static means that the variable or method becomes a part of the class and not an instance, but when do we use this? Also, if a variable is static, does that mean it is immutable like a final variable? Basically what is the use of Static?
You will usually use static when your variables are general and are not supposed to be part of each instance separately, take as an example the number pi, if you create a Math class, you will set pi as a static final variable, and also the methods will be declared as static, because you don't want to create a class instance everytime you want to use the power function. If your function doesn't use the properties of the class it will probably be static.
Another use I found useful, when you create a class within another, you should declare the inner class as static, otherwise you will need an instance to create instance of the inner class
This question already has answers here:
Why are interface variables static and final by default?
(16 answers)
Closed 8 years ago.
I am new to Java. I heard that Interface can have only static variables, is that correct?
Yes, because interfaces cannot be instantiated, so instance state would not make sense.
A quick Google search found the same answer.
http://www.coderanch.com/t/245071/java-programmer-SCJP/certification/final-member-variables-interfaces
This is correct. The reason is that Java interfaces cannot be instantiated by themselves. You must instantiate an instance of a class that implements the interface. Because of this, the value of the variables must be assigned in static context because in this case no instance will exist.
They also need to be declared as final to ensure that the value assigned to the interface variable is a true constant that cannot be changed at run-time by other program code.
As mentioned in the comment
public static final variables
1. Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists.
2. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.
I was just wondering if there is a reason why an object cannot be performed with a non-static variable? I am able to fix the error, (obviously) but I was just wondering why.
static variable initialized when class is loaded into JVM on the other hand instance variable has different value for each instances and they get created when instance of an object is created either by using new() operator or using reflection like Class.newInstance().
So if you try to access a non static variable without any instance compiler will complain because those variables are not yet created and they don't have any existence until an instance is created and they are associated with any instance. So in my opinion only reason which make sense to disallow non static or instance variable inside static context is non existence of instance.
Read more here
If you have a variable which is class-dependant and you try to reference it in a static method it won't compile due to the fact that non-static class variables need an instance to get initialized.
Static elements get initialized automatically into JVM when the class gets loaded - instance variables don't, they're initialized when an instance is created.
Take a look at some Java Docs regarding class variables, it's described in depth in the original Oracle manual. You can start by looking here for example.
This question already has answers here:
when exactly are we supposed to use "public static final String"?
(13 answers)
Closed 9 years ago.
I am a beginner in Java Programmer and do not understand what is this variable: public final static int ID = 8; please tell me a definition or example code. Thanks.
public - Any object can see it, even objects that use your code as a library.
final - its value will never change.
static - however many objects of this class you create, there will be only one.
int - it is a 32-bit integer.
ID - it can be referred to by this name.
= - it is immediately assigned the value.
8 - it will have the value 8 (decimal).
This form is commonly used for constant values. The compiler will often replace every access to it with its constant value instead.
public means that it can be accessed from other classes
final means it cannot be reinitialized i.e its value can't be changed after initialization.
static means that all instances of the class use the same exact field (unlike non-static fields where each instance has their own version of the field). static fields are described as being 'class variables' Similarly, non-static fields are called 'instance variables'
public means it accessible by any other class outside of that one.
final means once the variable is declared it cannot be changed.
static means the variable can be accessed from any method within the class.
int is a primitive data type declaration.
These are all basic concepts of Java OOP, so I'd recommend reading about it a little.
Declaring Variables: http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
This question already has answers here:
Why keyword 'this' cannot be used in a static method?
(13 answers)
Closed 9 years ago.
I knew that we can not use this keyword in a static method, But I got confused why we can not use this inside static blocks orstatic methods. Even the same case with super(). Could anyone shed light on this?
Thanks
this and super refer to the current instance and the parent instance respectively.
Within any static context, whether it is a static block or a static method, there is no instance to refer to, and therefore the keywords are not permitted.
super() is a call to the parent instance's no-arg constructor, and is only permitted as the first statement in a constructor, which disqualifies it from appearing in any static context.
According to the doc
Within an instance method or a constructor, this is a reference to the
current object — the object whose method or constructor is being
called. You can refer to any member of the current object from within
an instance method or a constructor by using this.
But the static methods are related to the Class not to the object. in static methods you dont have any instance.
Static methods and blocks are, by definition, linked to the class and not any instance of this class.
As this refers to the current object instance, it is perfectly normal that you cannot use it in any static initialization block or method.
Because this points to an instance of the class, in the static method/block you don't have an instance.
according to Oracle
Within an instance method or a constructor, this is a reference to the
current object — the object whose method or constructor is being
called.
So, in a static class you have NO instance variables-objects created. That's why you cannot use the this keyword
Static blocks can be used to initialize static variables for example.
Static merthods are not working within an instance scope either.
Both are related to only the class and have nothing to do with an instance of that class in any way.
To answer this we should consider what static means - it means that this bit is put in a 'static' place in memory, it exists only once.
So every class has static bits and non-static bits. The static bits exist once and the non-static bits can exist many times (i.e. lots of different instances). The word 'this' can then be thought of as shorthand for 'this version of the non-static bits'.
In a static context we have no version of the non-static bits to refer to (or to be more precise we don't know which version we should refer to!) so we have no this. The same argument can be made for super.