Class Method Example
This program defines method in class.
Problem Statement
Write a Java program to define method display() in Student class.
Source Code
| 1 | class Student { |
| 2 | String name; int age; |
| 3 | void display(){ |
| 4 | System.out.println("Name: "+name+", Age: "+age); |
| 5 | } |
| 6 | } |
| 7 | public class ClassMethod { |
| 8 | public static void main(String[] args) { |
| 9 | System.out.println("Eduinq Class Method"); |
| 10 | Student s=new Student(); |
| 11 | s.name="Arun"; s.age=20; |
| 12 | s.display(); |
| 13 | } |
| 14 | } |
Program Output
Eduinq Class Method Name: Arun, Age: 20
Explanation
Method display() prints attributes. - Object calls method. - Together demonstrates class method. Conclusion: Shows how to define and call method in class.