What is this variable public final static [duplicate] - java

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

Related

Why there's no error in this code? j is final and static but still accessible using object of class how? [duplicate]

This question already has an answer here:
How static fields are referenced through objects? [duplicate]
(1 answer)
Closed 2 years ago.
interface TempInterface {
static final int j = 30;
}
class TempClass implements TempInterface {
}
public class Try {
public static void main(String args[]) {
TempClass obj = new TempClass();
System.out.println(obj.j);
}
}
In Java, the static keyword means that the variable can be accessed by every object of a class. Java forces every variable in an interface to be both static and final, so every object of a class that implements it will be able to access that variable, but not change it.
A static variable is nothing more than a fixed memory location shared by all instances of the class (since it belongs to the class itself). This means that all of the instances can access it since they know its location. Note however that it is usually frowned upon if you access a static field from an instance instead of a class reference.
For further information I suggest you read up on Oracle's tutorial regarding class variables. To quote a section:
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
Regarding interfaces, you can think of them as special types of classes, so by implementing an interface you inherit all of its data as well (i.e. static variables).
Static means that you have access without need to create new instance, final means - you can'not change but you may get this.
And I can note what interface variable may be accesed from another package too because it's also implicitly public (as static and final).

Static Variables and Methods in Java [duplicate]

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

When is it better to use static variable? [duplicate]

This question already has answers here:
Difference between Static and final?
(11 answers)
Closed 5 years ago.
I am wondering why sometimes I see code with static variable? When is it better to use static variable instead of "normal" variable? If its value do not change from an istance to another, is it not better to use final variable?
The point in using static variables is not the same as final ones.
Final-declared variables (static or not) cannot be modified. They are often used as "reference values" or constants.
Static-variables (and methods) are therefore needed as "shared content". For instance, say each person in the office likes to drink coffee. Are we better of with everyone bringing his own coffee machine ? Or are we better sharing one such machine for the entire office ?
Obviously you want to chose the shared option. In a programming idiom, this would translate to having a static variable in the Office class representing the unique CoffeeMachine.
Off-topic but surely you wouldn't want to make this coffee machine final. What if someone breaks it ? You would need to replace it, and thus change the variable.
static means, that the variable is in all instances of this Object the same.
The Main example is a Object-Counter.
class foo{
private static int count = 0;
public foo()
{
count ++;
}
public static getCountOfObj()
{
return count;
}
}
So you can edit it on all foo-Objects.
static variables are used when only one copy of the variable is required. so if you declare variable inside the method there is no use of such variable it's become local to function only..
Variables declared static are commonly shared across all instances of a class.
I assume you mean static fields.
static fields are associated with the class, whereas instance fields are associated to an object (aka class instance).
If a field is marked as final (works for both instance and static fields), then it cannot be reassigned.
So each has its own different role to play.

Is it safe to assume that a static variable always exists [duplicate]

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).

Interface can have only static variables in JAVA? [duplicate]

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.

Categories

Resources