Attributes & Methods βΒΆ
Prerequisites: Java Classes & Objects
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' β