chapter 5
Chapter 6: Using Methods, Classes, and Objects
Objectives
Creating Methods: Learn to create methods with no parameters, a single parameter, and multiple parameters.
Return Values: Understand how to create methods that return values.
Class Concepts: Learn about class concepts and how to create a class.
Instance Methods: Create instance methods within a class.
Object Declaration: Declare objects and utilize their methods.
Organizing Classes: Understand the organization of classes and their constructors.
Classes as Data Types: Recognize that classes are data types in Java.
Creating Methods
General Information
Method Definition: A method is a program module that contains a series of statements to perform a task.
Calling Methods: A method can be invoked (called) from another method.
Parameters and Arguments: Methods can take parameters, which are specified in the method declaration.
Method Structure
Method Header: Contains:
Access modifiers (optional)
Return type
Method name
Parameter list (optional)
Method Body: Enclosed in curly braces
Implementation of the method occurs here.
Methods without a body are called stubs.
Types of Parameters
Zero, One, Multiple Parameters
Zero Parameters: Methods defined without any parameters.
Single Parameter: Takes one argument for processing.
Multiple Parameters: Can accept multiple arguments, separated by commas.
Parameter Declaration Example
public static void methodName(int param1, String param2) {
// method body
}Method Return Values
Return Type: Can be any Java type;
voidindicates no return.Return Statement: Used to return a value to the calling method.
Unreachable Statements: Occur after a return statement and cause compiler errors.
Calling a Method
Methods can call other methods which increases modularity and reusability.
Example:
public static double methodA(double amount) {
// calculation
return result;
}Class Concepts
Defining Classes
Class Structure: A class definition includes a name, data fields, and methods.
Header defines access level and name.
Methods identify behavior of objects.
Instance Variables
Instance Variables: Hold data specific to an instance of a class.
Access Modifiers: Control visibility (public, private).
Using Constructors
Constructor Purpose: A special method to create and initialize objects.
Default Constructor: Automatically created if no constructors are defined; initializes fields with default values.
Custom Constructor: Must match the class name and can initialize with specific values.
Data Hiding and Encapsulation
Encapsulation: Hides the internal state of an object and requires all interaction to be performed through methods.
Access Methods:
Setters (Mutators) to change data.
Getters (Accessors) to read data.
Organizing Classes
Organizing data fields and methods logically improves readability. Methods often listed after data fields.
Summary
Methods: Are foundational in structuring Java programs for reusability and simplicity.
Classes: Serve as blueprints for objects that encapsulate data and behavior.
Constructors: Provide initial values for object attributes and are crucial for instantiation.