Class is the core concept of java, because a java program is nothing but a class. So we will know about class in detail. Let's move forward.
Class classname { field declaration; Method declaration; }
Example
class MyClass { int x,y; void get(); void show(); }
Here in the above source code, the keyword class tells to the compiler that there is a class named as MyClass taking two int data members such as x and y and two methods such as get( ) and show( ) respectively.
See an example MyClass1.java;
class MyClass1 { int x=100; public static void main(String[] args) { MyClass1 obj=new MyClass1(); System.out.println(obj.x); } }
Output: 100
We can create multiple objects of one class. For example;
MyClass2.java class MyClass2 { int x=100; public static void main(String[] args) { MyClass2 ob1=new MyClass2(); MyClass2 ob2=new MyClass2(); System.out.println(ob1.x); System.out.println(ob2.x); } }
Output:
100
AreaTriangle.java class Area { double base, height; void get() { base = 5.2; height = 4.6; } void show() { double a; a = 0.5 * base * height; System.out.println("area is" + a); } } class AreaTriangle { public static void main(String[] args) { Area obj = new Area(); obj.get(); obj.show(); } }
Output:
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc