Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

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_no, age, 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

public class Dog {
   String breed;
   int age;
   String color;

   void barking() {
   }

   void hungry() {
   }

   void sleeping() {
   }
}

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.

class Dog {
  // Declaring and initializing the attributes
  String breed = "German Shepherd";
  int age = 2;
  String color = "Black";
}

public class Main {
  public static void main(String[] args) {
    // Creating an object of the class Dog
    Dog obj = new Dog();

    // Accessing class attributes & printing the values
    System.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.

class Dog {
  // Declaring and initializing the attributes
  String breed = "German Shepherd";
  int age = 2;
  String color = "Black";
}

public class Main {
  public static void main(String[] args) {
    // Creating an object of the class Dog
    Dog obj = new Dog();

    // Accessing class attributes & printing the values
    System.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";

    // Printing
    System.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.

package com.tutorialspoint;

class Dog {
   final String name = "Tommy";  
}

public class Tester {
   public static void main(String[] args) {
      Dog dog = new Dog();
      dog.name = "Tommy";  // Error while modifying name
      System.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)
Advertisements