Java Programming Fundamentals

Java Methods

Functions in Java

Known as methods, they are essential for organized, reusable, and manageable code.
A method is a block of code designed to perform a specific task.

Creating Our First Method

Define a method within a class.
Example: A method that prints a greeting message.

Key Components of a Java Method

Access Modifier: Controls visibility (e.g., public).
Return Type: Type of value the method returns (e.g., void if nothing is returned).
Method Name: Identifier used to call the method.
Parameters: Inputs to the method, enclosed in parentheses.
Method Body: Code that defines the task performed by the method.

Why Use Methods?

Promote code reuse, making programs more modular and easier to maintain.

Calling a Method in Java

Call a method from within the same class or another class.
Non-static methods require creating an instance of the class.

The main Method in Java

Serves as the entry point for Java applications.
Follows a specific structure: public static void main(String[] args).

Example of Method with Parameters and Return Type

Define a method that takes parameters and returns a value.
Example: A method that adds two numbers and returns the sum.

Key Points to Remember

Parameters: Inputs to the method.
Return Type: Type of value the method returns.
Method Call: Using an instance of the class to call the method.

Reusability of Methods

Reuse existing methods within other methods to avoid code duplication.
Example: A method that greets the user before performing an addition.

Static Methods

Static Methods

Belong to the class rather than any specific instance.
Can be called without creating an object.

Why Use Static?

Useful when the method doesn’t rely on the state of any object.
Access without creating an instance of the class.

Example with a Car Class

Define static and non-static methods within a class.
Static methods can access static variables directly.

Static in Memory

Stored in a shared area of the heap known as the method area.
Created when the class is loaded and destroyed when the class is unloaded.

Pass By Value vs. Pass By Reference

Pass-by-Value

The actual value of the variable is passed to a function.
Modifications inside the method do not affect the original variable.

Pass-by-Reference

Instead of passing a copy of the value, the memory address (or reference) of the variable is passed.
Changes to the object inside the method reflect on the original object.

Java's Approach: Pass-by-Value

For primitive data types, a copy of the value is passed.
For objects, a copy of the reference is passed, meaning changes to the object's fields affect the original.

Pure vs. Impure Methods

Pure Methods

Always produce the same output for the same input.
No observable side effects.

Impure Methods

May produce different outputs for the same input based on external factors.
Can cause side effects, such as modifying external states or interacting with external systems.

Debugging Methods in Java

Setting Conditional Breakpoints

Pause execution only when a specific condition is met.

Navigating Code with Debugging Commands

Use 'Step Into' to delve into methods.
Use 'Drop Frame' to backtrack without restarting the debug session.
Use 'Show Execution Point' to return to the paused execution point.

Evaluating Expressions On-the-Fly

Test how different inputs affect the method's output during the debug session using "Evaluate Expression".
Understanding these Java concepts helps in building efficient, modular, and maintainable software systems. For more detailed information, refer to the Notion link:
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.