Introduction to Classes and Objects, the new Operator, Instance variables

To begin with, it is important to understand class as a template for an object and also as a data type. Java provides flexibility when working with classes so that the user can use it in any way as required and in ways so as to derive the maximum benefits with least complexity.

The class does not simply encapsulate the main() method but is actually a storehouse of the properties and attributes that shall define the variables, methods, and objects inside the class. Thus, classes are more like blueprints from which the objects can be created.

An object, by definition, is an instance of a class. Classes are like a logical framework that defines the relationship between its members and objects are a reference to that class. Thus, you can access the members of the class using that reference or object of the class. These members/methods have some access specifier which allows them to be accessed in the restricted manner only.

Classes: General Form

A class is declared by the use of the keyword class. A class can contain any number of variables, methods, constructors, statements or expressions. Here is a general format for the declaration of a class:

class className {

   datatype variable1;
   datatype variable2;   //can define more variables

   access_specifier return_type method1() {
     //body of method
   }

   access_specifier return_type method2() {
    //body of method
   }
   
   // Inner classes etc
}

Creation of Objects

An important part of an object oriented programming is the creation of an object. The object that is created is an instance of the class or a reference to the class. This object contains a copy of all the members of the class and can be used to refer to the members of the class. As we already know that class is a sort of data type(user defined) and we use this data type for declaring objects of that class.

The declaration of an object is shown here:

className objectName;

This is simply the declaration of the object and no memory is allocated to it. Objects in Java are allocated dynamic memory using the keyword new. It allocates the memory and do the proper initialization of the object during run-time(dynamic allocation) and returns a reference to the object which is contained in objectName.

The above memory allocation format is shown below:

className objectName = new className();

Or it can be done in two steps as:

className objectName;
objectName = new className(); // Simply updating the reference

In this case, the first line declares the object and the second line creates the object and assign the value of the reference to the variable named objectName.


The “new” Keyword

As we have already seen that the new keyword is used for allocation of memory to objects. It also allocates memory to arrays or any other kind of object.

new keyword allocates memory dynamically,i.e. during run-time. The advantage of this is that the program shall create as many or as fewer objects according to requirement. The reason is that memory is finite and a case might arise that there’s insufficient memory for the creation of an object. In this case, an exception is thrown during run-time. The function of the new keyword is shown in the figure below:

Class and Object


Instance Variables

The variables defined within the class are known as instance variables. The variables are called so because the instance members of the class(objects) and every object contains its own copy of these variables(except the static ones).

These variables are defined outside of any method but can be accessed by any methods that are inside the class and operated upon. These variables are not defined as static and are object specific, thus, they are used by the objects to store their state or behavior. An example is shown below:

class className {

    Integer int1;
    datatype variable2;

    int method() {
        return new Integer(10);
    }

}


Program to show the creation of classes and objects

Unable to retrieve the Code part.
Please reload again.
Notify us if the problem still persists.
Till we work on this you can view code on URL below.
Please visit - https://github.com/HiteshGarg/codingeek/blob/master//Java/Classes/ObjectDemo.java
Output:-
The value of a is : 5 and b is : 6
The sum is : 11

Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Do come back for more. Share and subscribe.

Keep Coding!! Happy Coding!!  🙂

Recommended -

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
average guy
average guy
2 years ago

u don’t know how helpful this is !!! Thank u so much keep it up

1
0
Would love your thoughts, please comment.x
()
x
Index