WikiGalaxy

Personalize

Java Class Attributes

Introduction to Attributes:

Attributes in Java classes are variables that hold data. They define the state of an object and can be accessed and modified through methods.

Example 1: Basic Attribute Declaration

This example demonstrates how to declare attributes within a class.


class Car {
    String color;
    int year;
}
        

Explanation:

Here, the class Car has two attributes: color and year. These attributes describe characteristics of a car.

Access Modifiers for Attributes

Public vs Private Attributes:

Access modifiers determine the visibility of class attributes. The most common are public and private.

Example 2: Using Access Modifiers

This example shows the use of public and private modifiers.


class Person {
    public String name;
    private int age;
}
        

Explanation:

The name attribute is public, allowing access from outside the class, while age is private, restricting access to within the class itself.

Default Values of Attributes

Understanding Defaults:

Java provides default values for attributes if they are not explicitly initialized.

Example 3: Default Values

Here, we see what default values are assigned to different types of attributes.


class DefaultValues {
    int number; // Default value is 0
    boolean flag; // Default value is false
}
        

Explanation:

The integer number defaults to 0, and the boolean flag defaults to false.

Static vs Instance Attributes

Difference Between Static and Instance:

Static attributes belong to the class, whereas instance attributes belong to the object.

Example 4: Static and Instance Attributes

This example illustrates the declaration of static and instance attributes.


class Animal {
    static int population;
    String species;
}
        

Explanation:

The static attribute population is shared across all instances, while species is unique to each instance.

Final Attributes

Immutable Attributes:

Final attributes cannot be changed once initialized, ensuring immutability.

Example 5: Final Attributes

Demonstrating the use of final attributes in a class.


class Book {
    final String ISBN;
    Book(String isbn) {
        this.ISBN = isbn;
    }
}
        

Explanation:

The ISBN attribute is final, meaning it must be initialized in the constructor and cannot be changed thereafter.

Transient Attributes

Attributes Excluded from Serialization:

Transient attributes are not serialized, meaning they are not saved during the serialization process.

Example 6: Transient Attributes

Illustrating the use of transient attributes in a class.


class UserSession {
    transient String sessionId;
    String userName;
}
        

Explanation:

The sessionId attribute will not be serialized, making it suitable for sensitive data that should not be persisted.

Volatile Attributes

Thread-Safe Attributes:

Volatile attributes ensure visibility of changes to variables across threads.

Example 7: Volatile Attributes

Demonstrating the use of volatile attributes in a class.


class SharedData {
    volatile boolean isUpdated;
}
        

Explanation:

The isUpdated attribute is volatile, ensuring that updates to it are visible to all threads immediately.

Attribute Initialization

Initializing Attributes:

Attributes can be initialized directly or through constructors.

Example 8: Attribute Initialization

Illustrating different ways to initialize attributes.


class Laptop {
    String brand = "Dell";
    int ram;
    Laptop(int ramSize) {
        ram = ramSize;
    }
}
        

Explanation:

The brand attribute is initialized directly, while ram is initialized via the constructor.

Encapsulation with Attributes

Using Getters and Setters:

Encapsulation involves using methods to access and modify private attributes.

Example 9: Getters and Setters

Demonstrating encapsulation using getters and setters.


class Account {
    private double balance;
    
    public double getBalance() {
        return balance;
    }

    public void setBalance(double amount) {
        balance = amount;
    }
}
        

Explanation:

The balance attribute is private, with public methods getBalance and setBalance to access and modify it.

Attributes in Inheritance

Inherited Attributes:

Attributes can be inherited from a superclass, allowing reuse and extension.

Example 10: Inherited Attributes

Demonstrating inheritance of attributes from a superclass.


class Vehicle {
    String type;
}

class Car extends Vehicle {
    int wheels;
}
        

Explanation:

The Car class inherits the type attribute from Vehicle, demonstrating reuse of attributes through inheritance.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025