What is a class in Java? Explain how data fields and methods are declared and accessed in Java – Java Tutorial
Problem Statement
What is a class in Java? Explain how data fields and methods are declared and accessed in Java using a programming example.
Solution:
A class can be defined as an entity in which data and functions are put together. The concept of class is similar to the concept of structure in C. A class is declared by using the keyword class.
The general form of class is
class classname
{
type variable 1;
type vanable2;
type method(parameter-list)
{
}
type method2(paramteter-list)
{
}
type method n(paramteter-list)
{
}
}
The data lies within the class and the data fields are accessed by the methods of that class.
The data fields are also called instance variables or member variables because they are created when the objects get instantiated.
For example –
class Test
{
int a;
int b;
}
In object-oriented programming, any two objects communicate with each other using methods.
All the methods have the same general form as the method main(). Most of the methods are specified as either static or public.
Java classes do not need the method main, but if you want a particular class as a starting point for your program then the main method is included in that class.
The general form of method is –
type method(parameter-list)
{
}
The type specifies the type of data returned from the method. If the method does not return anything then its data type must be void.
For returning the data from the method the keyword return is used.
Objects are nothing but the instances of the class. Hence a block of memory gets allocated for these instance variables.
For creating objects in Java the operator new is used.
Test obj // Declaration of object obj.
obj = new Test () ; // obj gets instantiated for class Test
We instantiate one object obj; it can be represented graphically as –
Now we can two objects and instantiate them,
Test obj1, obj2;
obj1 = new Test ()
obj2 = new Test ()
It can be represented graphically as –
There are two types of class members – The data members and the method.
These members can be accessed using the dot operator.
For accessing the data members the syntax is –
name_of_object.variable_name = value;
For accessing the method of the class the syntax is
name_of_object.method_name(parameter_list);
For example –
obj.name=”XYZ”
obj.display();
Suppose we wish to pass the parameters to the method then first we will create the object for the class as follows –
Test obj1=new Test();
Test obj2=new Test();
Now two objects are created namely obj1 and obj2.
Obj1.get_val(11,22);
obj2.get_val(99,100);
Suppose by this method we assign values to two variables a and b then
It can be graphically represented as –
Sample Program to demonstrate, how to access data fields and methods
/*This is a Java program which shows the use of class in the program */ class Rectangle { int height; int width; void area() { int result=height*width; System.out.println('The area is "+result); } } //Another class in which main() function is written. class classDemo { public static void main(String args[]) { Rectangle obj=new Rectangle(); obj.height=10; //setting the attribute values obj.width=20; //from outside of class obj.area(); //using object method of class is called } }
OUTPUT
The area is 200
In the above program, we have used two classes one class is classDemo which is our usual one in which the main function is defined and the other class is Rectangle.
In this class, we have used height and width as attributes and one method area() for calculating the area of a rectangle. In the main function, we have declared an object of a class as
Rectangle obj=new Rectangle();
And now using obj we have assigned the values to the attributes of a class. The operator new is used to create an object.
The objects access the data fields and methods of the class using the dot operator. This operator is also known as the object member access operator.
This data field height and width are called instance variables.
And the method, area is referred to as the instance method. The object on which the instance method is invoked is known as the calling object.
Summary:
In this article, we understood What is a class in Java? Explain how data fields and methods are declared and accessed in Java using a programming example – Java Tutorial. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and the YouTube channel for video tutorials.