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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou 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
1Introduction
Introduction