Java Quiz 3: Classes, Objects, Methods, and Strings — Comprehensive Notes
Overview: Classes, Objects, Methods, and Strings
- This recording is an introduction to using classes, objects, methods, and strings in Java, building on the Intro to Java material.
- Example setup: create a new class called GradeBook and use it to create objects.
File naming and public classes
- A class declaration that begins with the keyword public must be stored in a file whose name matches the class name and ends with the .java extension.
- The keyword public is an access modifier indicating the class is available to the public.
- The Android app example is described as a package consisting of several classes, objects, and methods.
- It is possible to declare classes and methods without an access modifier (default/package-private), but that topic will be covered later.
Java access modifiers
- Three major access modifiers: public, private, and protected.
- public: the class or method is visible to and accessible from all other classes.
- private: restricts access to methods or variables to only the methods inside the same class.
- protected: between public and private; accessible within the same package and to subclasses.
Packages and implicit imports
- A package groups related classes and methods; it helps organize larger programs (e.g., an Android app is a package).
- You can declare classes and methods without an access modifier (default access); these belong to the same package.
- Some classes require no explicit import statements because they are part of Java’s built-in packages.
- java.lang (which includes String and System) is implicitly imported into every program.
- You may not need to import GradeBook if it’s in the same package.
The main method and driver classes
- Not every class contains a main method; only an executable class (a driver class) has the main method to run the program.
- A driver class tests new classes by calling their public methods.
- The main method is declared in a driver class and is the entry point for execution.
Static methods and object creation
- A static method can be called without creating an instance of the class in which it is declared.
- Usually, you cannot call a method that belongs to another class until you create an object of that class.
- In code, you declare a variable of a class type, e.g., GradeBook gradeBook1; using new creates a new object of that class.
- The parentheses to the right of a class name in a new expression indicate a call to a constructor, which initializes the new object.
- Each new object created from a class has its own copy of the class’s instance variables.
The GradeBook example: objects and methods
- After creating an object of class GradeBook and assigning it to a reference variable (e.g., myGradeBook), you can call its methods to perform tasks.
- You can modify your class to include a parameter in a method, such as making the displayMessage method take a parameter (e.g., a course name).
- Method parameters are declared in a comma-separated list inside the parentheses after the method name; each parameter has a type and a name.
- Every method call must supply arguments corresponding to its parameters; the number and types of arguments must match the parameter list.
- The GradeBookTest driver demonstrates passing user input to the GradeBook methods, showing how argument types must align with parameter types.
- The Scanner class is used to read user input from the keyboard; a new Scanner object is added in the driver class to obtain input at runtime.
- The imported classes in the driver do not need explicit import statements for String or System because they are part of java.lang.
- Since GradeBook is in the same package as GradeBookTest, it is implicitly available without an import.
Local variables vs instance variables (fields)
- Local variables are declared inside a method; they exist only during the method’s execution and are not automatically initialized.
- Instance variables (also called fields) are declared inside a class but outside any method; every object has its own copy.
- Instance variables are typically declared with private access to enforce encapsulation.
- Each object maintains its own copy of the instance variables; they are not shared across objects.
Default values and initialization
- If you do not initialize an instance variable yourself, Java assigns a default value.
- The default value for a field of type String is \text{null}.
- The default values for primitive types are:
- \text{byte}=0, \ \text{char}=0, \ \text{short}=0, \ \text{int}=0, \ \text{long}=0, \ \text{float}=0.0, \ \text{double}=0.0
- \text{boolean}=\text{false}
- Local variables do not get automatic initialization and must be assigned before use; otherwise, using them leads to errors.
Encapsulation: getters and setters
- Private instance variables are part of data hiding (information hiding): access is restricted to the class itself.
- Public methods are provided to allow clients to set or get private variables, preserving control over how variables are modified or accessed.
- Methods like setCourseName and getCourseName act as setters and getters, respectively.
- The order of method declarations in the class does not determine call order; one method can call another simply by its name.
- The naming convention for accessors/mutators is commonly: setters start with set, getters with get, though your naming does not have to strictly follow this convention, it is widely followed.
Access with respect to class members
- The instance variable courseName is an example of a field.
- Even though the field is not declared inside a method, it can be accessed by all methods within the class, and by methods of other objects via public getters/setters.
- The practice of making fields private and providing public getters/setters enforces encapsulation and prevents unintended external modification.
The role of constructors
- When an object is created with new, Java calls a constructor to initialize the object.
- If you do not declare any constructor, the compiler provides a default no-argument constructor that initializes fields to their default values.
- A constructor must have the same name as the class and has no return type (constructors do not return values).
- Constructors can be declared public; you can have multiple constructors with different parameter lists (constructor overloading).
- In the GradeBook example, a constructor with a parameter (e.g., a String value) is shown to initialize the object with specific data.
- The driver class demonstrates creating two GradeBook objects using the constructor, each with its own set of instance variable values.
Practical implications and takeaway
- Classes serve as blueprints; objects are concrete instances created from these blueprints.
- Access modifiers control visibility and accessibility across classes and packages.
- Encapsulation protects data integrity by restricting direct access to fields and exposing controlled interfaces via methods.
- Constructors provide flexible initialization for new objects and allow multiple ways to create objects with different initial states.
- Always consider how to test your classes: use a driver class with a main method to instantiate objects and exercise their methods.
- Read the book thoroughly for additional material and prepare for quiz/exam questions beyond this lecture.
Quick reference: key terms and syntax
- public class GradeBook: a class declaration stored in GradeBook.java
- public, private, protected: access modifiers controlling visibility
- package: a namespace that groups related classes; implicitly imported types from java.lang
- main method: entry point of a Java application, typically in a driver class; often public static void main(String[] args)
- static: a member that belongs to the class, not to any object
- new GradeBook(…): creates a new GradeBook object by calling the constructor
- constructor: special method to initialize a new object; has the same name as the class; no return type
- this.courseName: example of using an instance variable inside methods
- getter: a method like getCourseName() that returns an instance variable
- setter: a method like setCourseName(String name) that assigns a new value to an instance variable
- String: a reference type; default value is \text{null}
- primitive types: boolean, byte, char, short, int, long, float, double with default values as specified above
- Scanner: used to read user input; often created in the driver class to supply arguments to methods