Home > DeveloperSection > Blogs > Class Concepts in Java (Part-1)

Class Concepts in Java (Part-1)

Java Java  OOPS  Software Development 
0 Comment(s)
 129  View(s)
Rate this:

Class Concepts in Java (Part-1)


A class is a template that encapsulates data and the methods that operate on this data, as in the Employee or Automobile class described earlier. A class is a user-defined data type that embeds methods to operate on the enclosed data. You create objects based on this template, and you use the new keyword in Java to create an object (in which case, we say that the class is instantiated).

You can create many objects from one single class. All these objects have the same type with a predefined set of data members. The values held in these data members vary from object to object. Although each object is similar to another object of the same type, each object has its own unique identity. For example, two cars of the same type and colour will have two different vehicle identification numbers (VINs) as their unique identifiers.

Defining a Class

The general form for defining a class is as follows:

Modifiers class ClassName




To define a class, we use the class keyword. The ClassName is a valid identifier that defines a unique name for a class. This name should be unique within the entire application. However, we may use the same name for classes belonging to two independent applications or Java packages.

The modifier in front of the class keyword defines the visibility of the class. Do not worry about this modifier right now.

A typical class declaration looks like this:

class MyClass {

        // attributes

       // constructors

      // methods


The class definition is enclosed in opening and closing braces. Within the body of the class definition, we define:

·         zero or more attributes

·         zero or more constructors

·         zero or more methods

The attributes of a class are its data members. These are also called fields.

·    The fields provide the state of the class and its objects. For example, for an Employee class, as described earlier, ID is its field.

·         A class constructor is a special method of the class that we use while initializing new objects.

·         A class may contain other methods that define the behavior of the class and its objects.

Example: Point Class:

The following code snippet shows a declaration for the Point class.

class Point


    int x;

    int y;




·         The class is defined using the keyword class followed by its name, Point.

·    The modifier field is optional and is not applied in this definition. The modifier defines the class visibility .

·         The body of the class is enclosed in braces. The body of this class consists only of fields.

·         Our Point class declaration contains two fields, x and y, of type int.

·         This simple Point class definition does not contain any methods.

As mentioned earlier, methods define the functionality of a class. Our Point class currently does not exhibit any functionality. As shown in this example, it is possible to create a class that does not contain any methods and has only fields. It is also possible to create a class that has an empty body—meaning no fields, no constructors, and no methods. However, creating such a class is usually meaningless, except for the name and/or whether it inherits from another one.

Don't want to miss updates? Please click the below button!

Follow MindStick