Objects are the real entity in the world and class is the framework of objects. Therefore we can say classes and objects are interrelated topic in real life and java. Any member of the class can access by the objects of that class. only static member of the function can access through the class name.Static variable are known as class variable and a non-static variables known as object variables.
Any real entity of the world which has its own unique characteristic, state and behavior are called an object.
Ex. a Pen, a ball, you, me, a cat, a chair, a TV etc.
Class is a collection of same types of objects. We can declare a class by the keyword class.
Ex. All of us are belongs to the Human class. In other words any objects of the Human class are refer a person of the world.
Java class consist two parts
Note : Every Java Program should write with in a class. And we can access the member variable and member function by using the object of that class.
Here we create a class called Human. And also we create the object of that class from another class called AccessHuman
class Human
{
private int age,height;
public void setValues(int a,int h);
{
age=a;
height=h;
}
public void getValues()
{
System.out.println(“Your Age is “ + age);
System.out.println(“Your Height is “ + height);
}
}
class AccessHuman
{
public static void main(String []args)
{
Human h1=new Human(); // Object Creation
h1.setValues(15,4); // Access Methods
h1.getValues(); // By Object
}
}
Class | Object |
A class is a blueprint of a real world object. It contains instance variables and methods.
|
An object is an instance of a class.
|
A class exists in the memory of a computer. |
An object does not exist in memory in a computer. |
There will be only one copy of a class. |
Multiple objects can be instantiated from the same class. |