static vs final in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

static vs final in Java

explain the basic difference between static and final with examples.

26th Jul 2017, 1:33 PM
Sridhar
Sridhar - avatar
4 Answers
+ 4
final: Once a final variable has been assigned, it always contains the same value - it can not be changed static: a static variable can change as often as you re-asign a value - and you do not need to object of a class to access it, you can access it using the class-name An example is the value PI of the class Math, you can simple access it with Math.PI withouth the need of creating a Object of the Math Class A combination of both is possible, let's take PI of the Java Math class again as example. If you look up the source code of java.lang.Math you will see: public static final double PI = 3.14159265358979323846; which means: - PI can be accessed without the need of having an object of the Math class since it is static - PI can not be changed because it is final
26th Jul 2017, 2:06 PM
Martin Ed
Martin Ed - avatar
+ 2
Some stuff that was not said: static actually makes the member bounce to the class itself. As a result, you cannot override them (But can 'hide' them). Since it becomes bound to the class you can also access it how @Martin put it. (Although, you don't need to do that unless you're in a different class). * Keep in mind, inside a static method you cannot access a non-static method without an instance of the class.* final for variables is exactly as @Martin put it. Final methods on the other hand, cannot be overrided, and cannot be hidden. Final classes cannot be sub-classed. This is because all members become final, so the purpose of a sub-class becomes meaningless.
26th Jul 2017, 3:46 PM
Rrestoring faith
Rrestoring faith - avatar
0
OK gd answer.
26th Jul 2017, 2:04 PM
Sridhar
Sridhar - avatar
0
Got a clarity on overriding (static and final). thank-you for ur time
26th Jul 2017, 3:50 PM
Sridhar
Sridhar - avatar