Types of Methods - 9.2 | 9. Methods and Constructors | ICSE Class 11 Computer Applications
K12 Students

Academics

AI-Powered learning for Grades 8–12, aligned with major Indian and international curricula.

Academics
Professionals

Professional Courses

Industry-relevant training in Business, Technology, and Design to help professionals and graduates upskill for real-world careers.

Professional Courses
Games

Interactive Games

Fun, engaging games to boost memory, math fluency, typing speed, and English skillsβ€”perfect for learners of all ages.

games

Interactive Audio Lesson

Listen to a student-teacher conversation explaining the topic in a relatable way.

Instance Methods

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Today, we will begin with instance methods. Can anyone tell me what an instance method is?

Student 1
Student 1

I think instance methods are related to objects of a class!

Teacher
Teacher

Exactly! Instance methods can access instance variables of the object. They require an instance of the class to be used. Can someone provide an example?

Student 2
Student 2

In the Calculator class, the multiply method is an instance method, right?

Teacher
Teacher

Yes! Good example! Let’s dig deeper. Why do you think instance methods are important?

Student 3
Student 3

They help manage state and behavior specific to an object!

Teacher
Teacher

Great! They support encapsulation and promote code reuse. Remember 'I' for 'Instance' in instance methods, which signifies their connection to objects.

Static Methods

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Moving on to static methods, does anyone know how they differ from instance methods?

Student 4
Student 4

Static methods belong to the class itself, not to any object!

Teacher
Teacher

Correct! And which variables can static methods access?

Student 1
Student 1

Only static variables and other static methods.

Teacher
Teacher

That’s right! For example, our MathUtils class has a static square method. Why might we use a static method?

Student 2
Student 2

To perform a utility function that doesn't require object state!

Teacher
Teacher

Exactly! Static methods promote modularity. A mnemonic to remember is 'SS': 'Static for the Class'.

Void Methods

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Next, let’s discuss void methods. Who can tell me what makes a method 'void'?

Student 3
Student 3

It doesn't return any value!

Teacher
Teacher

Perfect! Can anyone give an example of where we might use a void method?

Student 4
Student 4

The Printer class has a printMessage method that prints to the console.

Teacher
Teacher

Yes! Void methods are useful for actions rather than calculations. Remember 'V' for 'Void' means 'Value not returned'.

Review of Methods

Unlock Audio Lesson

Signup and Enroll to the course for listening the Audio Lesson

0:00
Teacher
Teacher

Let’s summarize what we learned about the methods. Give me the key types we discussed.

Student 1
Student 1

Instance methods!

Student 3
Student 3

Static methods!

Student 2
Student 2

Void methods!

Teacher
Teacher

Correct! Remember, instance methods need an object, static methods belong to classes, and void methods don’t return anything. Review these definitions because each plays a crucial role in Java programming.

Introduction & Overview

Read a summary of the section's main ideas. Choose from Basic, Medium, or Detailed.

Quick Overview

This section explores different types of methods in Java, including instance methods, static methods, and void methods, and their significance in programming.

Standard

In this section, we discuss the three primary types of methods in Java: instance methods that operate on object instances, static methods that belong to the class, and void methods that do not return values. Understanding these types helps in writing modular and organized code, enhancing reusability and maintainability.

Detailed

Types of Methods in Java

Java methods can be categorized based on how they operate and their relationship to class instances. This section discusses three main types of methods:

1. Instance Methods

Instance methods are designed to operate on instance variables of an object. They require an object of the class to be invoked, and they can access both instance and static variables. For example:

Code Editor - java

These methods can perform actions specific to the instances of the class.

2. Static Methods

Static methods belong to the class itself, rather than to any specific instance. They can be invoked without creating an instance and can only access static variables and static methods. An example is:

Code Editor - java

Static methods are useful for operations that don't require object state.

3. Void Methods

Void methods do not return any value. They perform actions without producing a return result. An example is:

Code Editor - java

These methods are often used for executing a task that doesn’t require feedback.

Youtube Videos

Constructor One Shot | ICSE Class 10 2023 | Notes | Theory + Programming
Constructor One Shot | ICSE Class 10 2023 | Notes | Theory + Programming
Constructor in Java | ICSE Class 10 Constructor Chapter 4 | One Shot | Semester 1 | Computer
Constructor in Java | ICSE Class 10 Constructor Chapter 4 | One Shot | Semester 1 | Computer
CONSTRUCTORS  | Lecture 1 | Default | Parameterized | Non-Parameterized  | ICSE 10 | Anjali Ma'am
CONSTRUCTORS | Lecture 1 | Default | Parameterized | Non-Parameterized | ICSE 10 | Anjali Ma'am
ICSE 10th Computer Application || Constructor in Java
ICSE 10th Computer Application || Constructor in Java

Audio Book

Dive deep into the subject with an immersive audiobook experience.

Instance Methods

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

● Instance methods operate on the instance variables of an object and require an instance (object) of the class to be invoked. They are called on objects and can access both instance variables and static variables.
Example:
class Calculator {
int multiply(int a, int b) {
return a * b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.multiply(4, 5)); // Output: 20
}
}

Detailed Explanation

Instance methods are functions defined within a class that operate on the class's instance variables. To use an instance method, you need to create an object of the class. For instance, in the provided example, the Calculator class has a method called multiply. An object of the Calculator class (calc) is created in the Main class. When calc.multiply(4, 5) is called, it computes the product of 4 and 5, returning 20. This demonstrates that instance methods require an object to invoke them and they can manipulate or access the data held by that object.

Examples & Analogies

Think of instance methods like a chef in a restaurant who uses specific tools (like knives or pots) to cook food. The chef (instance of a class) can only use his tools (instance variables) when he is working in the kitchen (object). Just like the chef needs to be present to prepare meals, an instance method needs an object to function.

Static Methods

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

● Static methods belong to the class rather than to any specific instance of the class. They can be called using the class name without creating an object of the class. They cannot access instance variables but can access static variables and other static methods.
Example:
class MathUtils {
static int square(int x) {
return x * x;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.square(4)); // Output: 16
}
}

Detailed Explanation

Static methods are associated with the class itself rather than any individual object of the class. This means you can call a static method without creating an object. In the MathUtils class, the method square calculates the square of a number. You can directly call this method using the class name (MathUtils.square(4)), and it will return 16. Importantly, static methods cannot interact with instance variables since they do not belong to any specific object.

Examples & Analogies

Consider static methods like a factory that produces items (functions) irrespective of individual orders. The factory (class) can release an item (method) to anyone without needing to have a personal connection through an employee (object). Just like anyone can buy products from the factory without needing to know the employees, anyone can use static methods directly by referencing the class.

Void Methods

Unlock Audio Book

Signup and Enroll to the course for listening the Audio Book

● A method that doesn’t return any value is called a void method. It simply performs an action without returning any result.
Example:
class Printer {
void printMessage() {
System.out.println("Hello, World!");
}
}
public class Main {
public static void main(String[] args) {
Printer p = new Printer();
p.printMessage(); // Output: Hello, World!
}
}

Detailed Explanation

Void methods are those that perform an action but do not return any value. In the Printer class, the printMessage method prints "Hello, World!" to the console. Because this method is declared as void, it does not give back any value after execution. To invoke this method, an object of the Printer class is created, and the method is called. This method simply focuses on performing the action of printing without any return type.

Examples & Analogies

Think of a void method like a street performer who entertains people. The performer (method) does not 'give back' something in return, such as money or a gift; instead, they provide entertainment (action) directly. Just like you enjoy the performance without expecting anything in return, a void method executes its function without providing a value.

Definitions & Key Concepts

Learn essential terms and foundational ideas that form the basis of the topic.

Key Concepts

  • Instance Methods: Operate on object instances and require an object to invoke.

  • Static Methods: Belong to the class itself and are called without an object.

  • Void Methods: Perform an operation without returning a value.

Examples & Real-Life Applications

See how the concepts apply in real-world scenarios to understand their practical implications.

Examples

  • Instance Method Example: The multiply method from the Calculator class.

  • Static Method Example: The square method in the MathUtils class.

  • Void Method Example: The printMessage method in the Printer class.

Memory Aids

Use mnemonics, acronyms, or visual cues to help remember key information more easily.

🎡 Rhymes Time

  • Instance is for the object state, Static is the class that's great, Void does tasks without a return, Learn these types, now it's your turn!

πŸ“– Fascinating Stories

  • Imagine a library (class) where each book (instance) has its own identifier. The librarian (instance method) helps find each book individually. There's a general guide (static method) for all books and notices (void methods) that don't need feedback.

🧠 Other Memory Gems

  • 'IVS' - Instance for the object, Static for the class, and Void means no return; just tasks to surpass.

🎯 Super Acronyms

ISV

  • 'I' for Instance
  • 'S' for Static
  • 'V' for Void to remember their nature.

Flash Cards

Review key concepts with flashcards.

Glossary of Terms

Review the Definitions for terms.

  • Term: Instance Methods

    Definition:

    Methods that operate on instance variables and require an instance of the class to be invoked.

  • Term: Static Methods

    Definition:

    Methods that belong to the class and can be called without creating an instance. They cannot access instance variables.

  • Term: Void Methods

    Definition:

    Methods that do not return any value and are used for performing actions.