L3-Methods Classes and Objects
Integrative Programming and Technologies
Author: Denver Novencido
Understanding Method Calls and Placement
A method is a program module containing statements that perform a task.
The main() method can call additional methods, which can call other methods.
Any class can contain multiple methods that can be called repeatedly.
Advantages of Method Usage
Clarity: Separate methods keep the main() method concise, enhancing readability.
Reusability: Methods like displayAddress() can be reused in any application needing that functionality.
Method Construction
Every method consists of:
Method Header: Indicates how other methods can interact with it.
Method Body: Contains the implementation within curly braces.
Components of Method Header
Usually includes:
Optional access specifier
Return type
Identifier (method name)
Parentheses (may contain parameters)
Access Specifiers
Java methods can have access specifiers: public, private, protected, or package.
Public Access: Allows methods to be accessed by any class.
Static Modifier: Indicates methods that can be called without instantiating an object.
Return Types
Determines the type of data sent back to the calling method.
A method with no return value has a return type of void.
E.g.,
void displayAddress().
Method Naming Conventions
A method's name:
Must be a single word and cannot be a Java keyword.
Cannot contain spaces.
Parentheses in Method Headers
Parentheses may accept data (arguments) sent to the method.
E.g., main() contains
String[] args.
Adding Parameters to Methods
Arguments are sent during method calls; parameters receive these arguments.
Implementation hiding adds flexibility and abstracts method details from users.
Single Parameter Methods
Methods can receive parameters by specifying:
Parameter type
Local name for parameter.
Example:
public static void calculateGross(double hours).
Multiple Parameters in Methods
Methods can take multiple parameters to enhance functionality.
Returning Values from Methods
A method can return values, complete, throw exceptions, or reach a return statement.
Chaining Method Calls
Methods can call other methods, creating a chain of executions.
Classes and Objects
Class vs Object: A class is an abstract template, while an object is an instance of a class, embodying its attributes and methods.
Creating a Class
A class is defined by a name and its data and methods.
Example header:
public class Employee.
Organizing Classes
Classes often include multiple data fields and methods for better structure.
Declaring Objects
To create an object:
Declare the type and identifier (e.g.,
Employee someEmployee).Use the new operator to allocate memory (e.g.,
someEmployee = new Employee();).
Overloading Methods
Overloading allows multiple methods with the same name but different parameter lists.
Constructors
Default constructors require no arguments and are created by the compiler.
Custom constructors can be created for more control over object instantiation.
Using the 'this' Reference
The 'this' reference allows access to the object’s current attributes and methods.
Automatically Imported Classes
Java provides numerous prewritten classes called packages to avoid redundant code.
The
java.langpackage is imported in every program.