Java Class Attributes
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Class Attributes

Jeevadharshan

Java Class Attributes (Fields)  

In Java, variables declared inside a class are known as attributes or fields. They represent the state (data) of an object and define its characteristics. For example, a Person class may contain attributes like name, age, and city. 
 
Key point:  Attributes belong to a class, and each object created from that class gets its own copy of those attributes. 

Declaring Attributes in a Class 

You define attributes inside a class but outside any method. 
 
public class Rectangle { 
    int width = 10; 
    int height = 5; 
 
Here, width and height are attributes of the Rectangle class. 

Accessing Attributes 

To access an attribute, create an object of the class and use the dot (.) operator. 
 
public class Rectangle { 
    int width = 10; 
 
    public static void main(String[] args) { 
        Rectangle rect = new Rectangle(); 
        System.out.println("Width: " + rect.width); 
    } 
}  

Output 

Width: 10 

Modifying Attribute Values 

Attributes can be changed after object creation. 
 
public class Rectangle { 
 
    int width; 
 
    public static void main(String[] args) { 
        Rectangle rect = new Rectangle(); 
        rect.width = 25;   // modify attribute 
        System.out.println(rect.width); 
    } 
}  

Output 

25

Overriding Default Values 

If an attribute has an initial value, you can override it for a specific object. 
 
public class Product { 
    double price = 99.99; 
 
    public static void main(String[] args) { 
        Product item = new Product(); 
        item.price = 79.99;  // override default value 
        System.out.println(item.price); 
    } 
}  

Using final Attributes (Constants) 

If you want an attribute’s value to remain constant, declare it with the final modifier. Such attributes cannot be reassigned. 
 
public class MathConstants { 
    final double PI = 3.14159; 
 
    public static void main(String[] args) { 
        MathConstants m = new MathConstants(); 
        System.out.println(m.PI); 
    } 
 
Attempting to change PI would cause a compile-time error.  

Attributes and Multiple Objects 

Each object maintains its own attribute values. Changing one object does not affect another. 
 
public class Counter { 
    int value = 5; 
 
    public static void main(String[] args) { 
        Counter c1 = new Counter(); 
        Counter c2 = new Counter(); 
 
        c2.value = 20;  // change only c2 
 
        System.out.println(c1.value); 
        System.out.println(c2.value); 
    } 
}  

Output 

20

Classes with Multiple Attributes  

Classes often contain several attributes representing a complete entity. 
 
public class Employee { 
    String firstName = "Riya"; 
    String lastName = "Sharma"; 
    int age = 28; 
 
    public static void main(String[] args) { 
        Employee emp = new Employee(); 
 
        System.out.println("Name: " + emp.firstName + " " + emp.lastName); 
        System.out.println("Age: " + emp.age); 
    } 
}
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !