AP Computer Science A: Using Objects and Methods
1. Classes, Objects, State, and Behavior
• A class is a blueprint that defines the attributes (fields) and behaviors (methods) of objects.
• An object is an instance of a class with its own unique state.
• State = instance variables; Behavior = methods that operate on state.
• Example:
public class Car {
private String make;
private int year;
public Car(String m, int y) {
make = m;
year = y;
}
}
2. Creating and Storing Objects (Instantiation)
• Objects are created using the 'new' keyword followed by a constructor call.
• Constructors initialize object attributes and have no return type.
• If no constructor is defined, Java provides a default no-argument constructor.
• Objects are stored in heap memory and referenced by variables.
• Example:
Car car1 = new Car("Toyota", 2022);
3. Instance vs Static Members
• Instance variables belong to individual objects; static variables belong to the class.
• Instance methods operate on specific objects, static methods are called on the class.
• Example:
double result = Math.sqrt(16); // static method call
4. Method Calls, Parameters, and Return Values
• Methods define behavior; they can be void (no return) or return a value.
• Parameters allow data to be passed to methods; arguments must match the declared types.
• Example:
public int getYear() {
return year;
}
5. Using Library Classes
• Java provides built-in classes like String, Math, and ArrayList.
• Examples:
String text = "Hello";
int length = text.length();
String upper = text.toUpperCase();
double power = Math.pow(2, 3);
ArrayList<String> names = new ArrayList<>();
names.add("Alice");
names.add("Bob");
6. Method Overloading and Constructors
• Overloading allows multiple methods or constructors with the same name but different parameters.
• Example:
public class Book {
private String title;
private double price;
public Book(String title) {
this.title = title;
this.price = 0.0;
}
public Book(String title, double price) {
this.title = title;
this.price = price;
}
}
7. Reference vs Primitive Types
• Primitive types store actual values (int, double, boolean).
• Reference types store memory addresses of objects.
• Calling a method on a null reference throws NullPointerException.
Car car4 = null;
car4.getMake(); // runtime error – NullPointerException
8. Access Control and Encapsulation
• Encapsulation protects data using private variables and public getter/setter methods.
class Person {
private String name;
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
9. Common Errors
• Forgetting 'new' when creating objects.
• Using wrong parameter types or order in method calls.
• Confusing instance vs static members.
• Calling methods on null references.
10. Example Code
public class Student {
private String name;
private int grade;
public Student(String n, int g) {
name = n;
grade = g;
}
public void updateGrade(int newG) {
grade = newG;
}
public String getInfo() {
return name + " has grade " + grade;
}
}
// Usage example
Student s = new Student("Luis", 10);
s.updateGrade(11);
System.out.println(s.getInfo()); // “Luis has grade 11”