Menu
Coddy logo textTech

Access Attributes

Lesson 9 of 12 in Coddy's Basics of Classes and Objects in Java course.

To access attributes in an object, we use:

MyClass myObj = new MyClass();
System.out.println(myObj.attr);

In the above example, we access the attribute called attr (and print it) in the object myObj which is an instance of MyClass class.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You are given a code with a class named SomeClass, in the main method, create an object for this class and print the two attributes it has line by line.

Try it yourself

class SomeClass {
    String foo = "something";
    String bar = "cool";
}

class Main {
    public static void main(String[] args) {
        // Write code here
    }
}

All lessons in Basics of Classes and Objects in Java