Example - Changing the value of class attribute -
Output :
A class attribute can be of many types we will discuss them as follows -
Types of Class Attributes
Class attribute can be made following types -
1. static
2. final
A static class attribute can be accessed directly by className.AttributeName.
Syntax -
class className{ |
Example -
Output :
Java’s final keyword has slightly different meanings depending on the context, but in general it says “This cannot be changed.” You might want to prevent changes for two reasons: design or efficiency. Because these two reasons are quite different, it’s possible to misuse the final keyword.
When final is used with object references rather than primitives, the meaning can be confusing. With a primitive, final makes the value a constant, but with an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified; Java does not provide a way to make any arbitrary object a constant. (You can, however, write your class so that objects have the effect of being constant.) This restriction includes arrays, which are also objects.
Final is a keyword hence it will be written in small case.
Syntax :
class className{ |
Example -
Output :