Friday 29 July 2016

Class and Object in Java



Class: - Class is the collection similar object. It is just like template which is used to create an object. Class is the smallest unit (all are under class) in Java.
Or
Class is the collection of data member and member functions data types are known as data member and functions are known as member function or method. Class is the smallest unit (all are under class) in Java.

Object: - An object is the instance of the class. Instance means, the object of the class on which we are currently working. In Java the entire object are dynamic. All the object in Java are created by using new operator and are created in special memory called heap.
Syntax: - Class name reference name =new class name(); 

(Reference name mean name of the object)                object

Example: -
//W.A.P for calling of object with different way
class first1
{
public String name;
public void my()
{
System.out.println("Name is "+name);
}
}
class withoutstatic
{
public static void main(String args[])
{
first1 f;
f = new first1();
f.name = "Safal";
f.my();
first1 f1 = new first1();
f1.name = "Sahil";
f1.my();
}
}
OUTPUT:-
Name is Safal
Name is Sahil

learn More

No comments:

Post a Comment