Module lessons (1/2)
Classes and Objects
Java is a language focused on Object-Oriented Programming (OOP). In OOP, we model the real world using objects. An object has state (data) and behaviors (actions).
Classes and Objects
- Class: It is a blueprint (or template) that defines the properties and behavior of a type of object.
- Object: It is a concrete instance created from a class.
For example, if Car is the class, your car in the garage is the actual object.
Instance Fields and Constructors
The data inside a class is called instance fields (or attributes). To initialize the state of an object at the moment of its creation, we use a special method called a constructor.
A constructor has the same name as the class and has no return type (not even void).
class Dog {
// Instance fields
String name;
int age;
// Constructor
public Dog(String name, int age) {
this.name = name; // 'this' refers to the current object's field
this.age = age;
}
}
Creating an Object with new
To create an object in memory, we use the new keyword followed by the class constructor:
public class Main {
public static void main(String[] args) {
// Creating a Dog object
Dog myDog = new Dog("Fido", 3);
// Accessing fields
System.out.println(myDog.name + " is " + myDog.age + " years old.");
}
}
The this Reference
The this keyword in Java refers to the current object on which the constructor or a method is being executed. It is primarily used to avoid ambiguity when constructor parameters have the same name as the object's instance fields (variable shadowing):
class User {
String username;
public User(String username) {
this.username = username; // this.username is the instance field, username is the parameter
}
}
Memory Allocation: Stack vs Heap
Understanding how Java allocates memory is essential:
- Stack: It is a fast and organized memory area where primitive variables and object references (memory addresses) local to methods are stored.
- Heap: It is a larger and dynamic memory area where actual objects are allocated (using the
newkeyword).
When we declare Dog myDog = new Dog("Fido", 3);:
- The reference
myDogis created on the Stack. - The actual
Dogobject with its data is created on the Heap. - The
myDogvariable points to the location of the object on the Heap.
Try it yourself
Complete the Person class by adding the field name and an appropriate constructor. Then, in the main method, create a Person object named p with the name Alice.
Show hint
Write `String name;` inside the class, declare the constructor `public Person(String name) { this.name = name; }` and instantiate it with `new Person('Alice')`.
Solution available after 3 attempts
Create a Book class with the fields title (String) and pages (int), a complete constructor, and instantiate in the main method a book named b with the title Moby Dick and 600 pages.
Show hint
Remember to define both fields in Book, map them in the constructor using `this.title = title;` and `this.pages = pages;`, and use `new Book('Moby Dick', 600)`.
Solution available after 3 attempts
Create a Product class with fields name (String) and price (double), and a constructor that initializes both fields. In the main method, create a Product object named p with name Laptop and price 999.99.
Show hint
Declare `String name;` and `double price;` fields in Product, assign them in the constructor with `this.name = name;` and `this.price = price;`, and finally create the object in the main method.
Solution available after 3 attempts