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

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.

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

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

What is this variable public final static [duplicate]

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 keyword and static [duplicate]

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.

Categories

Resources