Ch 04: How Objects Behave
Source: Head First Java, Second Edition | Pages: 105-128
🎯 Learning Objectives
Methods, parameters, and encapsulation
📚 Key Concepts
Method parameters and arguments
Return types
Getters and setters
Encapsulation basics
Instance variables vs local variables
Comparing objects
== vs equals()
📖 Detailed Notes
1. Method parameters and arguments
Essential concept for mastering Java and OOP.
Example:
2. Return types
Essential concept for mastering Java and OOP.
Example:
3. Getters and setters
Essential concept for mastering Java and OOP.
Example:
4. Encapsulation basics
Essential concept for mastering Java and OOP.
Example:
5. Instance variables vs local variables
Essential concept for mastering Java and OOP.
Example:
6. Comparing objects
Essential concept for mastering Java and OOP.
Example:
7. == vs equals()
Essential concept for mastering Java and OOP.
Example:
💡 Important Points to Remember
part: If a method takes a parameter, you must pass
Java
moment, we’ve
a class describes what an
Java is pass by value, (which
✅ Self-Check Questions
Test your understanding:
Can you explain the main concepts covered in this chapter?
Can you write code examples demonstrating these concepts?
Do you understand when and why to use these features?
Can you explain the benefits and tradeoffs?
🔄 Quick Revision Points
📝 Practice Exercises
Write your own code examples for each key concept
Modify existing examples to test edge cases
Explain concepts to someone else
Create a small project using these concepts
🔗 Related Chapters
Review related concepts from other chapters to build comprehensive understanding.
For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 105-128.
Chapter 4: How Objects Behave — Study Notes
This chapter focuses on the relationship between an object's state (instance variables) and its behavior (methods). You will learn how to pass data to methods, get data back, and how to protect that data through encapsulation.
1. State Affects Behavior
Every instance of a class has its own values for instance variables. Because methods use these variables, objects of the same type can behave differently based on their state.
Instance Variables: Represent what an object knows (its state).
Methods: Represent what an object does (its behavior).
Example: A
Dogobject with asizeof 70 might have abark()method that prints "Woof! Woof!", while aDogwith asizeof 8 prints "Yip! Yip!".
2. Sending Things to a Method (Parameters)
You can pass values into a method to tell it more about what you want it to do.
Parameters: The variables defined in the method signature that receive values (e.g.,
void bark(int numOfBarks)).Arguments: The actual values you pass into the method when calling it (e.g.,
d.bark(3)).Type Safety: You must pass the correct type of value that the method expects. If a method takes an
int, you cannot pass it aString.
3. Getting Things Back from a Method (Return Values)
Methods can return a value back to the caller.
Return Type: Every method must declare what it returns. If it returns nothing, it is marked
void.Return Statement: If a method has a return type other than
void, it must use thereturnkeyword followed by a value of that type.
Java
4. Java is "Pass-by-Value"
In Java, when you pass an argument to a method, you are passing a copy of the bits inside the variable.
Primitives: The method gets a copy of the actual value. If the method changes that value, the original variable outside the method remains unchanged.
References: The method gets a copy of the reference (the "remote control"). This means the method and the caller both point to the same object on the heap. If the method changes the object's state, those changes are visible to the caller.
5. Encapsulation (Data Hiding)
To keep your code safe and flexible, you should hide an object's instance variables and provide access only through methods.
The Rule: Mark your instance variables as
privateand providepublicgetters and setters.Why it Matters:
Validation: You can prevent invalid data (e.g., setting a
Dog's size to a negative number).Flexibility: You can change how data is stored or calculated internally without breaking other people's code that uses your class.
Java
6. Local vs. Instance Variables
Instance Variables: Declared inside a class but outside any method. They have default values (0 for numbers,
nullfor references) and live as long as the object lives.Local Variables: Declared inside a method. They do not have default values and must be initialized before use. They live only as long as the method is on the stack.
7. Comparing Variables
==Operator: Used to compare the "bits" inside two variables.For primitives, it checks if the values are the same (e.g.,
5 == 5).For references, it checks if two reference variables point to the exact same object on the heap.
.equals()Method: Used to check if two different objects are meaningfully equivalent (e.g., two differentStringobjects containing the same characters).
Last updated