+ 3
what is the difference between static and final?
4 Réponses
+ 13
When an attribute or method is declared as "static". It means that its a class attribute/method
So it belongs to the class rather than a specific instance. You can access those class members via the class name.
When final is used in class declaration is means that particilar class cant be inherited.
When final is used with attributes, it means the value is constant and cant be changed.
When final is used with methods, it means that method cant be overriden.
+ 5
When we use use "final" key word with method thats mean we declare it as a constant and when we change its value, error accor.
And when we use "static" key word with any method, its mean this method belongs to the class and we can access this by class
0
it is meaningless to compare them. They are completely different things. Static makes your variable belongs to class and you can reach this variable from every instance of that class. If you add final keyword that makes your static variable to a constant. It is not a variable anymore, it is a constant and has an only one value. So, you can reach that constant every instant of this class.
- 9
idk