A class in java is a blue print or a template for a object. it is basic entity of object oriented system. A class has a group of object that has common property. a class in java contain data member, method, constant, block, class and interface. A class is a way of binding the data and associated methods in a single unit. Whenever we start executing a java program, the class will be loaded into main memory with the help of class loader subsystem (a part of JVM) only once. A class defines the structure and behavior (data and code) that will be shared by a set of objects. When you create a class, you will specify the code and data that constitute that class. Collectively, these elements are called members of the class.
Syntax:class <class name> { Variable declaration Method declaration }
Here class is a keyword which is used for developing or creating user define datatype. Class names are used for creating objects.
Exampleclass AddingTwoInts { public static void main(String[] args) { int a = 60; int b = 70; int c = a + b; System.out.println("The addtion of " + a + " and " + b + " is " + c); } }
A object is a basic runtime entity of object orient system. Or it is a instance of class. If we want to store the data for the data members of the class, we should create an object. Instance it is known as instance of class. The real world entity are called as object. For creating memory space in java we used new operator.
SyntaxClassname objname = new Classname();
Whenever any object declared by its class name then such type of class name is called as declared object. Such type of object does not have memory allocation.
Example.Rectangle r
Whenever any object is created by using new operator then such type of object is called as allocated object.
Example.r = new Rectangle(); Rectangle r = new Rectangle();
A object that does not have its own memory but it refer to the another allocated object then such object is called as reference object.
ExampleRectangle r1 = new Rectangle(); Rectangle r2 = r1;