Java Class Methods
Java Class Methods
Class methods are functions defined inside a class that describe the behavior or actions of objects. They operate on class attributes or perform specific tasks.
1. Method Syntax
modifier →
public,private,static, etc.returnType → data type of value returned (
voidif nothing is returned)methodName → name of the method
parameters → optional input values
2. Example 1: Instance Method
📌 Output:
Instance methods operate on instance variables and require objects to be called.
3. Example 2: Static Method
📌 Output:
Static methods belong to the class, not the object, and can be called using the class name.
4. Example 3: Method with Parameters and Return Value
📌 Output:
5. Example 4: Method Calling Another Method
📌 Output:
6. Key Points About Class Methods
| Feature | Instance Method | Static Method |
|---|---|---|
| Belongs to | Object | Class |
| Access instance variables? | Yes | No (only static variables) |
| Call syntax | object.method() | Class.method() |
| Can be overridden | Yes | No (cannot override static methods) |
Methods define object behavior.
Instance methods need objects; static methods do not.
Methods can have parameters and return values.
