Attributes & Methods โ¶
Mentor's Note: Think of a class like a Smartwatch. The hardware features (Battery %, Color, Storage) are the Attributes. What the watch actually DOES (Sync data, Count steps, Set alarm) are the Methods. ๐ก
๐ The Scenario: The Fitness Smartwatch โ¶
Every smartwatch has its own data and its own features.
- The Attributes (The Data):
batteryLevel= 85%;stepsCount= 5000;isWaterproof= true;
- The Methods (The Actions):
syncWithPhone(): Uploads your data to the cloud.resetCounter(): Sets your steps back to zero.
- The Result: You use the Dot Operator (
.) to access the watch's battery or tell it to sync! โ
๐จ Visual Logic: Data vs Action¶
| Category | Role | Analogy | Java Term |
|---|---|---|---|
| Attributes | What it is | The physical parts ๐ฆ | Variables |
| Methods | What it does | The functions/apps โ๏ธ | Functions |
graph LR
subgraph Object: ["Smartwatch โ"]
A[stepsCount: 5000]
B[batteryLevel: 85]
C[syncWithPhone()]
D[resetSteps()]
end
๐ Concept Explanation¶
1. Accessing Attributes¶
In Java, we use the Dot Operator (.) to access an object's variables.
2. The final Keyword ๐ซ¶
If you don't want an attribute's value to be changed (like the watchID), use the final keyword. It makes the variable constant.
3. Static vs. Public Methods¶
- Static: These methods belong to the Class. You can use them without buying the watch (e.g., checking the global "Watch Compatibility" list).
- Public: These methods belong to the Object. You MUST own the watch (create an object) to use them.
๐ป Implementation: The Smartwatch Lab¶
// ๐ Scenario: A Smartwatch with data and actions
// ๐ Action: Using attributes and methods together
class Smartwatch {
// ๐ฆ Attributes
int batteryPercent = 100;
final String serialNumber = "SN-9988"; // Constant
// โ๏ธ Public Method (Requires an object)
public void syncData() {
System.out.println("Syncing your steps to the cloud... โ๏ธ");
}
// โ๏ธ Static Method (Belongs to the class)
static void showFactoryLocation() {
System.out.println("Factory Location: California, USA ๐ญ");
}
}
public class Main {
public static void main(String[] args) {
// ๐ญ Using a static method without an object
Smartwatch.showFactoryLocation();
// ๐๏ธ Creating the object
Smartwatch myWatch = new Smartwatch();
// ๐ท๏ธ Accessing and modifying attributes
myWatch.batteryPercent = 95;
// myWatch.serialNumber = "NEW-123"; // โ ERROR: final cannot be changed
// ๐ Calling a public method
myWatch.syncData();
}
}
๐ Sample Dry Run (Logic)¶
| Instruction | Computer's Action | Result |
|---|---|---|
Smartwatch.showFactoryLocation() |
Look up the blueprint directly | Prints "Factory Location" ๐ญ |
new Smartwatch() |
Allocate memory for batteryPercent |
Object created ๐๏ธ |
myWatch.syncData() |
Find the syncData code in the blueprint |
Prints "Syncing..." โ๏ธ |
๐ Technical Analysis¶
- Instance Variables: Attributes that are not
static. Every object gets its own copy. If I have two watches, changing the battery on one doesn't affect the other. - Class Variables (Static): Only one copy exists for all objects. They are shared across the entire class.
๐ฏ Practice Lab ๐งช¶
Task: The Music Player
Task: Create a class MusicPlayer with an attribute songTitle and a method playSong(). Create a static method aboutApp() that prints the version name.
Goal: Call both methods in the main class. ๐ก
๐ก Interview Tip ๐¶
"Interviewers often ask: 'Can a static method access an instance attribute?' Answer: NO. A static method doesn't know which object you are talking about. It belongs to the class blueprint, not the individual instance."
๐ก Pro Tip: "Attributes define the state, Methods define the behavior. Together, they make a complete Object!" - Vishnu Damwala
โ Back: Classes & Objects | Next: Constructors & 'this' โ