Java – Class Explanation

Java – Class Explanation 2017-08-09T10:05:38+00:00

Java Class:

Class is a collection of data members and member functions.

Now what are data members?

Data members are nothing but simply variables that we declare inside the class so it called data member of that particular class.

Now what are member functions?

Member functions are the function or you can say methods which we declare inside the class so it called member function of that particular class.

The most important thing to understand about a class is that it defines a new data type. Once defined, this new type can be used to create objects of that type.

Thus, a class is a template for an object, and an object is an instance of a class. Because an object is an instance of a class, you will often see the two words object and instance used interchangeably.

Syntax of class:

When you define a class, you declare its exact form and nature. You do this by specifying the data that it contains and the code that operates on that data.

The data, or variables, defined within a class are called instance variables. The code is contained within methods.

NOTE : C++ programmers will notice that the class declaration and the implementation of the methods are stored in the same place and not defined separately.



Output :
Printing the coordinates
10  20

Here x and y are data members of class MyPoint and displayPoint() is a member function of the same class.

In above program we can take as many objects as we want.

From the above program one thing we should keep in mind that when we want to access data member or methods of class we have to write objectname.member name


accessing data member of the class: objectname.datamember name;

accessing methods of the class: objectname.method name();

So for accessing data of the class: we have to use (.) dot operator.

NOTE: we can use or access data of any particular class without using (.) dot operator from inside that particular class only.

Prev Next