What is meant by a Method. Explain with reference to a class?
Advertisements
Question
What is meant by a Method. Explain with reference to a class?
Explain
Advertisements
Solution
A Method is a block of statements defined within a class that performs a specific task or operation. In Java, methods are used to define the behaviour of objects. A method is executed when it is called or invoked.
Explanation with Reference to a Class:
- A method is declared inside a class and contains statements to perform a particular operation.
- A class can contain one or more methods to define the behaviour of its objects.
- Methods can access and modify the data members of a class, depending on their access permissions.
- Methods help in code reusability, reduce repetition and make programs easier to understand and maintain.
class Student
{
void display()
{
System.out.println("Welcome to Java");
}
}
In the above example, Student is the name of the class, and display() is a method defined inside it. The method displays the message "Welcome to Java" when it is called.
shaalaa.com
Is there an error in this question or solution?
