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!
Class attributes
symbolize an object's properties. For instance, the attributes of a CellPhone
might be its brand
and model
:
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:
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:
Just as we humans can perform various tasks, our objects can possess more than one method. For instance, let's add another method, hangUp
:
To run this code, we can instantiate a class and call a method for the created instance:
Methods can interact with and modify class attributes. This can be illustrated with an updateModel
method that updates the model
:
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
.
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!
