Lesson 2

Unveiling Class Attributes and Methods in Java

Topic Overview and Actualization

Welcome back! In this course, we delve into Class Attributes and Methods in Java. These define an object's properties and behaviors. Our goal is to master the definition and application of these within a class. Through the construction of a CellPhone class, we will navigate this exciting journey!

Understanding Class Attributes

Class attributes symbolize an object's properties. For instance, the attributes of a CellPhone might be its brand and model:

Java
1public class CellPhone { 2 public String brand; // Brand of the phone 3 public String model; // Model of the phone 4}

Sometimes, attributes remain constant after initial set-up. The final keyword aids in creating such attributes. Let's add a chargerType attribute, which will be constant:

Java
1public class CellPhone { 2 public String brand; 3 public String model; 4 public final String chargerType = "Type-C"; // A constant attribute 5}
Understanding Class Methods

Methods are a class's behaviors, while attributes are its properties. For our CellPhone, a callDial method might be used to start a phone call:

Java
1public class CellPhone { 2 public String brand = "iPhone"; 3 public String model = "15 Pro Max"; 4 public final String chargerType = "Type-C"; 5 6 public void callDial(long phoneNumber) { 7 // Calling the given phone number, including class attributes 8 System.out.println("Dialing " + phoneNumber + " from my " + brand + " " + model); 9 } 10}

Just as we humans can perform various tasks, our objects can possess more than one method. For instance, let's add another method, hangUp:

Java
1public void hangUp() { 2 // Call has ended 3 System.out.println("Call ended."); 4}

To run this code, we can instantiate a class and call a method for the created instance:

Java
1CellPhone cellPhone = new CellPhone(); 2cellPhone.callDial("+1-212-456-7890"); // Prints: Dialing +1-212-456-7890 from my iPhone 15 Pro Max 3cellPhone.hangUp(); // Prints: Call ended.
Modifying Class Attributes

Methods can interact with and modify class attributes. This can be illustrated with an updateModel method that updates the model:

Java
1public void updateModel(String newModel) { 2 // Updating the model attribute 3 this.model = newModel; 4 System.out.println("Model updated to " + newModel); 5}

Here, the this keyword refers to the current instance of the class object. It means that we are setting the model field in the current CellPhone class object to newModel.

Lesson Summary and Practice

This concludes our lesson on class attributes and methods in Java. We have discussed class attributes and explored the creation of publicly accessible and constant attributes. Furthermore, we have studied class methods and their interaction with class attributes.

Now, it's time for you to enter the practice arena. Mastering attributes and methods will take you one step further in your Java learning adventure. Happy practicing!

Enjoy this lesson? Now it's time to practice with Cosmo!

Practice is how you turn knowledge into actual skills.