What is meant by a Method. Explain with reference to a class?
Advertisements
प्रश्न
What is meant by a Method. Explain with reference to a class?
स्पष्ट करा
Advertisements
उत्तर
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
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
