Slide 1
Slide 2
Slide 3

Class attribute in java programming (V53)

No Comments

Class Attributes


Class attributes are variables that are bound within a class. For example, variables that are used to define a class are called class attributes.

Class attributes define the state of a class during program execution. Class attributes are available to be used inside class methods by default.

For example, there is a class "Student" which has some data members (variables) such as roll_no, age, and name. These data members are considered as class attributes.

Creating (Declaring) Class Attributes

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

Syntax:


access_modifier: public, private, protected, or default (package-private)
static: optional; makes the attribute belong to the class instead of instances
final: optional; makes the attribute constant (cannot be changed)
data_type: the type of data (e.g., int, String, boolean)
attribute_name: the name of the attribute
value: optional; the initial or starting value

Create Project Name: DeclareAndAccess

Source Code: Student.java

Copied!
public class Student {
    // Instance attributes (belong to each object)
    public String name;

    // Static attribute (shared across all objects)
    public static String school = "ABC High School";

    // Final attribute (constant)
    public final String country = "Cambodia";
    
    public static final String COUNTRY = "USA"; // Constant shared by all students
  
        private int age;

    // Setter method
    public void setAge(int a) {
        age = a;
    }

    // Getter method
    public int getAge() {
        return age;
    }
    
}

Source Code: StudentAge.java

Copied!
class Student1 {
    private int age;

    // Setter method
    public void setAge(int a) {
        age = a;
    }

    // Getter method
    public int getAge() {
        return age;
    }
}

public class StudentAge {
    public static void main(String[] args) {
        Student1 s1 = new Student1();
        s1.setAge(21);                          // Set value
        System.out.println(s1.getAge());        // Get value → 21
    }
}

Source Code: DeclareAndAccess.java

Copied!
public class DeclareAndAccess {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Student s1 = new Student();     // Create object
        s1.name = "Alice";              // Access public attribute
        s1.setAge(21);                          // Set value 

        System.out.println("Student Name: " +s1.name);    // Output: Alice
        System.out.println("Age: " +s1.getAge());        // Get value → 21
        
        Student s2 = new Student();// Create object
        s2.name = "Bob";  // Access public attribute
         s2.setAge(18);                          // Set value
         
        System.out.println("Student Name: " +s2.name);    // Output: Bob
        System.out.println("Age: " +s2.getAge());        // Get value → 18
        
        System.out.println("They are Studying at  " +Student.school); // Access static attribute
        System.out.println(s1.name + " is from " + s1.country); // Access static attribute
        System.out.println(s2.name + " is from " +Student.COUNTRY); // Access static final attribute
    }
}

Result:

Watch the video:

back to top