DMCA.com Protection Status Java Programming Lesson 7 Part-2 | Functions / Methods in Java for Beginners - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 7 Part-2 | Functions / Methods in Java for Beginners

LESSON-7 PART-2

Functions / Methods in Java



Welcome to Lesson 7 Part-2 of the Java Programming Series!
In this lesson, you will learn one of the most important foundations of Java (and all programming):

✅ Here is Lesson-7: Functions / Methods in Java — Simple to Advanced 🚀

Methods help you write cleaner, reusable, and organized code. As a beginner, mastering methods will make your Java journey far easier and more enjoyable.


📘 Lesson-7: Functions / Methods

A method is a block of code that performs a specific task and can be reused.


✅ Why Methods?

✔ Avoid repeating code
✔ Make programs cleaner
✔ Easier to debug
✔ Improve organization


✅ Syntax of a Method

returnType methodName(parameters) { // code return value; // (only if returnType is not void) }

Example:

void greet() { System.out.println("Hello Java!"); }

✅ Types of Methods

1️⃣ Method with No return, No parameters

public static void greet() { System.out.println("Welcome to Java!"); }

Call:

greet();

2️⃣ Method with parameters

static void greetUser(String name) { System.out.println("Hello, " + name); }

Call:

greetUser("Jitu");

3️⃣ Method with return value

static int add(int a, int b) { return a + b; }

Call:

int result = add(10, 20); System.out.println(result);

4️⃣ Method Overloading

Same method name but different parameters ✅

static int add(int a, int b) { return a + b; } static double add(double a, double b) { return a + b; }

✅ Full Example Program

public class Main { static void sayHello() { System.out.println("Hello Java"); } static String fullName(String first, String last) { return first + " " + last; } static int multiply(int a, int b) { return a * b; } public static void main(String[] args) { sayHello(); System.out.println(fullName("S.M.", "JITU HASAN")); int result = multiply(5, 4); System.out.println("Multiplication = " + result); } }

✅ Method Calling Rules

Feature       Keyword
Used inside main without creating object     static
Needs object to call         Non-static

Example (non-static):

class Test { void msg() { System.out.println("Hello"); } public static void main(String[] args) { Test t = new Test(); t.msg(); } }

🧠 KEY TAKEAWAYS

✔ Methods = Reusable code
✔ Parameters send data → return values bring data back
✔ Static methods → direct call
✔ Non-static methods → require object
✔ Method Overloading allowed ✅


📝 Practice Exercise

Write a program with:

  • Method add() → returns sum
  • Method sub() → returns subtraction
  • Method display() → prints your full name

📌 Then call them from main()


⭐ Conclusion

In Java Lesson 7 Part-2, you learned:

✔ What methods are

✔ How to create and call them

✔ How parameters and return types work

✔ Method overloading

✔ Static vs non-static methods

✔ How to build cleaner, reusable Java programs

Methods are one of the most important Java fundamentals — master them now, and your future topics like OOP, classes, and objects will become much easier.


✅ Lesson-7 PART-2 Completed!



➡ NEXT Lesson-7 Part-3: Scanner Input (more)


Frequently Asked Questions (FAQs)

What is a method or function in Java?

A method is a block of code that performs a specific task. Methods help make code reusable, organized, and easier to maintain.

Why should we use methods in Java?

Methods reduce code duplication, improve readability, split complex programs into smaller parts, and make debugging easier.

What is the basic structure of a Java method?

A method usually consists of: return type → method name → parameters → method body.

What is the difference between parameters and arguments?

Parameters are variables defined in the method, while arguments are the actual values passed to the method when it is called.

What is a void method?

A void method does not return any value. It performs a specific action without producing a return value.

What is method overloading?

Method overloading is when multiple methods share the same name but have different parameter lists, allowing flexibility in method calls.

What is the purpose of the return keyword?

The return keyword sends a value from a method back to the caller and ends the method execution.

Where are methods defined in Java?

All methods are defined inside a class because Java is a fully object-oriented programming language.

2 comments:

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.