Class & Object



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
W.A.P for using static or call by class or without creating object
class first
{
public static String name;
public static void my()
{
System.out.println("Name is "+name);
}
}
class withstatic
{
public static void main(String args[])
{
//first f;
//f = new first();
//f.name = "Safal";
//f.my();
//first f1 = new first();
//f1.name = "Sahil";
//f1.my();
first.name="Safal";
first.my();
}
}
OUTPUT
Name is safal

No comments:

Post a Comment