Java Class Attributes With Examples

Java Class Attributes

Class Attributes
As we have seen already the data members of a class is also called Class attributes.
Class attributes are declared inside the boundary of a class and outside the boundary of all the methods.
We can also change the value of a class attribute with the help of class object, and with ( . ) dot symbol.
Dot symbol “.” is used to access the class members, like data members, or member methods.


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

  • Static class attribute

         A static class attribute can be accessed directly by className.AttributeName.

       Syntax -

class className{
static dataType variableName;



Example -

 Output :

  •  Final class attribute :

    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{
final dataType variableName;



Example -

Output :