Class Method and Field in Java with Example
Here you will get the program code to understanding the Class method and field in java with example.
In Java, a class serves as a blueprint for objects. Methods are functions within a class that define behaviors, while fields are variables storing object-specific data.
Class Method and Field in Java with Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //define volume class class volume { //Instance variables or fields int l,w,h; //display data method void display() { System.out.println("volume is"); System.out.println(l*w*h); } } class classDemo { public static void main(String str[]) { volume obj1=new volume(); volume obj2=new volume(); obj1.l=10; obj1.w=15; obj1.h=20; obj2.l=11; obj2.w=15; obj2.h=18; obj1.display(); obj2.display(); } } |
Output
C:\CodeRevise\java>javac classDemo.java
C:\CodeRevise\java>java classDemo
volume is
3000
volume is
2970
Check out our other Java Programming Examples