In Java Class is a user defined data type that allows you to bind data and its associated code together in a single unit.
- When you are defining a class you are actually creating a new data type. Using this newly created data type you can create a variable of its type which is known as object. Thus object is a variable of type class.
- Thus in the relationship of class and object a class acts as a template for an object which contains variables and methods, and an object acts as an instance of a class that access variables and methods defined inside the class.
The General Syntax of a defining a Class is given below:
class ClassName
{
Data type instance-variable1;
Data type instance-variable2;
………………………………………………
………………………………………………
Data type instance-variableN;
Return type MethodName1 (Parameter-list)
{
}
Return type MethodName2 (Parameter-list)
{
}
……………………………………………………………………..
……………………………………………………………………..
Return type MethodNameN (Parameter-list)
{
}
}
The class can be created using the keyword class.
The class contains two things:
(1) Variable Declaration
(2) Function Definition
- The variables that are defined within a class are known as instance variables.
- The functions defined inside the class are known as methods.
- The methods and Instance variables defined inside a class are called members of the class.
- While declaring instance variables and defining methods inside the class you can also specify access specifier for them such as public, private and protected. By default members of the class are public.
Example:
- Lets create a class named Rectangle that contains two instance variable named length and width and one method area () as follow:
class Rectangle
{
double length;
double width;
double area ()
{
return length*width;
}
}
- In the above example by defining the class you have created a new data type called Rectangle. Now using this new data type we can create a variable of that type which is known as object.
- When we declare the class memory is not allocated to the instance variables of the class because class declaration just acts as a template. In order to allocate memory to the instance variable we have to create an object of type class Rectangle as shown below:
Rectangle R1 = new Rectangle ();
- Here, R1 is the object or instance of class Rectangle. When we create object memory is allocated to the instance variables of the class. For each object of the class memory is allocated separately.
- Once you create an object of the class you can access instance variables and method of the class using object name followed by dot operator followed by instance variable or method as follow:
ObjectName.InstanceVariable= Value;
ObjectName.MethodName ();
Example:
R1.length = 35;
R1.width = 56;
Consider the Following example that calculate Area of Rectangle:
class Rectangle
{
double length;
double width;
void setValue(double l, double w)
{
length=l;
width=w;
}
double area()
{
return length*width;
}
}
class Test
{
public static void main (String args[])
{
Rectangle R1 = new Rectangle ();
R1.setValue (35, 42);
System.out.println ("Area of Rectangle= " + R1.area ());
}
}
ConversionConversion EmoticonEmoticon