Rucete ✏ AP Computer Science A In a Nutshell
2. Classes and Objects
This chapter introduces fundamental concepts of Java classes and objects, covering object characteristics, encapsulation, access control (public, private, static), methods, constructors, scope, references, and parameter passing.
Objects
• An object combines data (state) and operations (behavior).
• Example: A Book object has a title, author (state) and can perform operations like isOnShelf(), getAuthor(), etc.
• Objects are represented by object references in Java.
Classes
• A class is a blueprint for creating objects.
• A class encapsulates data fields (instance variables) and methods that operate on the data.
• Data encapsulation bundles state and behavior together.
Public, Private, and Static
• public: Accessible from outside the class.
• private: Accessible only within the class (used for instance variables).
• static variable: Shared by all instances of a class, memory allocated once.
• static final: Constant shared by all instances, value cannot be changed.
Methods
• Methods define behaviors or operations of a class.
Constructors
• Constructors create and initialize new objects.
• No-argument constructor: Provides default values.
• Parameterized constructor: Initializes object with specific values.
Accessors and Mutators
• Accessor methods: Retrieve object data without changing it (e.g., getBalance()).
• Mutator methods: Modify object state (e.g., deposit(), withdraw()).
Static Methods
• Operate at the class level, not tied to any particular object instance.
• Invoked with the class name (e.g., BankAccount.getInterestRate()).
• Main method must be static since no objects exist at program start.
Method Overloading
• Multiple methods with the same name but different parameter lists.
• Return type is not part of method signature for overloading.
Scope Rules
• Scope: The region of code where a variable can be accessed.
• Local variables are declared inside methods and are only accessible within those methods.
• Instance variables (fields) are accessible by all methods within the class.
• Variables with the same name at different scopes: local scope overrides instance variables temporarily.
Reference vs. Primitive Types
• Primitive types (int, double, boolean) store actual values directly.
• Reference types (objects) store memory addresses pointing to the actual object.
• Assigning one object reference to another makes them point to the same object, not a copy.
Passing Parameters
• Primitive types are passed by value: a copy of the value is sent to the method.
• Object references are also passed by value: the reference itself is copied, but both original and parameter reference the same object.
• Changes made to an object inside a method affect the original object; changes to primitives do not affect the original outside the method.
Null References
• A null reference points to no object at all.
• Trying to call a method or access a field on a null reference causes a NullPointerException.
• Always check if an object reference is null before using it.
Using the this Keyword
• this refers to the current object instance.
• Useful for distinguishing between instance variables and parameters with the same name.
• Example: this.balance = balance; sets the instance variable using the parameter value.
Garbage Collection
• Java automatically reclaims memory occupied by objects that are no longer referenced.
• Programmers do not manually delete objects in Java.
Good Class Design Principles
• Keep fields private to enforce encapsulation.
• Provide public accessor and mutator methods as needed.
• Initialize all fields properly in constructors.
• Use descriptive names for classes, methods, and variables.
• Avoid hard-coding values; use constants where appropriate.
In a Nutshell
Mastering classes and objects is essential for structured Java programming. Classes encapsulate data and behaviors, while objects represent real-world entities. Understanding scope, static features, constructors, reference types, and proper class design helps create clear, modular, and maintainable programs that leverage the full power of object-oriented programming.