class class_name { instance variable1; instance variable2; ….. method1() { method body.. } method2() { method body.. } ……… } |
class Cars { String car_name; int speed; long price; } |
class_name Object_name = new class_name(); |
Cars audi = new Cars(); //object 1 |
Cars audi; audi = new Cars(); Cars maruti = new Cars(); //object 2 |
Note1 : In the above example, statements Cars audi & audi = new Cars() are different. Cars audi ; // declaring a variable of a class that is object
Example : Objects In Java |
Note 2 : Each object defined will have the similar copies of fields.
In above example both audi and maruti object will have a copy of variables car_name, speed and price.
Example : Objects In Java |
Cars honda = new Cars(); Cars hyundai = honda; |
audi.speed = 300; audi.price = 2000000; |
object.method_name(); |
audi.run() |