Java – Class Attributes

Java Class Attributes

Java class attributes are the variables that are bound in a class i.e., the variables which are used to define a class are class attributes.

A class attribute defines the state of the class during program execution. A class attribute is accessible within class methods by default.

For example, there is a class “Student” with some data members (variables) like roll_noage, and name. These data members are considered class attributes.

Creating (Declaring) Java Class Attributes

To create (declare) a class attribute, use the access modifier followed by the data type and attribute name. It’s similar to declaring a variable.

Syntax

Use the below syntax to declare a class attribute:

access_modifier type attribute_name;

Example: Declaring Java Class Attributes

publicclassDog{String breed;int age;String color;voidbarking(){}voidhungry(){}voidsleeping(){}}

In above class, we’ve fields like breed, age, and color which are also known as class attributes.

Accessing Java Class Attributes

To access the class attribute, you need to create an object first and then use the dot (.) operator with the object name. Class attributes can be also called within the class methods directly.

Syntax

Use the below syntax to access a class attribute:

object_name.attribute_name;

Example: Accessing Java Class Attributes

Consider this example, demonstrating how to access the class attributes.

classDog{// Declaring and initializing the attributesString breed ="German Shepherd";int age =2;String color ="Black";}publicclassMain{publicstaticvoidmain(String[] args){// Creating an object of the class DogDog obj =newDog();// Accessing class attributes & printing the valuesSystem.out.println(obj.breed);System.out.println(obj.age);System.out.println(obj.color);}}

Output

German Shepherd
2
Black

Modifying Java Class Attributes

To modify a class attribute, access the attribute and assign a new value using the assignment (=) operator.

Syntax

Use the below syntax to modify a class attribute:

object_name.attribute_name  = new_value;

Example: Modifying Java Class Attributes

Consider this example, demonstrating how to modify the class attributes.

classDog{// Declaring and initializing the attributesString breed ="German Shepherd";int age =2;String color ="Black";}publicclassMain{publicstaticvoidmain(String[] args){// Creating an object of the class DogDog obj =newDog();// Accessing class attributes & printing the valuesSystem.out.println("Before modifying:");System.out.println(obj.breed);System.out.println(obj.age);System.out.println(obj.color);// Modifying class attributes
    obj.breed ="Golden Retriever";
    obj.age =3;
    obj.color ="Golden";// PrintingSystem.out.println("\nAfter modifying:");System.out.println(obj.breed);System.out.println(obj.age);System.out.println(obj.color);}}

Output

Before modifying:
German Shepherd
2
Black

After modifying:
Golden Retriever
3
Golden

Making Java Class Attributes Read Only

You can also make the class attributes read-only by using the final keyword after the access modifier while declaring an attribute.

Syntax

Use the below syntax to make class attribute read-only:

access_modifier final data_type attribute_name;

Example: Making Java Class Attributes Read Only

In the below example, the name attribute is set to read-only using the final keyword. Now this attribute can not be modified and JVM will complain if we try to modify this attribute.

packagecom.tutorialspoint;classDog{finalString name ="Tommy";}publicclassTester{publicstaticvoidmain(String[] args){Dog dog =newDog();
      dog.name ="Tommy";// Error while modifying nameSystem.out.println(dog.name);}}

Output

Compile and run Tester. This will produce the following result −

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The final field Dog.name cannot be assigned

	at com.tutorialspoint.Tester.main(Tester.java:10)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *