Closer look at – ‘new’, ‘this’ and ‘static’ keyword Java Day – 10

Published by Maneet Srivastav on

The new Keyword

The new operator dynamically allocates memory for an object. In the context of an assignment, it has this general form:

class-var = new classname ( );

Here, class-var is a variable of the class type being created. The classname is the name of the class that is being instantiated. The class name followed by  parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created. Constructors are an important part of all classes and have many significant attributes. Most real-world classes explicitly define their own constructors within their class definition. However, if no  explicit constructor is specified, then Java will automatically supply a default constructor.

You might be wondering why we do not need to use new for such things as integers or characters. The answer is that Java’s primitive types are not implemented as objects. Rather, they are implemented as “normal” variables. This is done in the interest of efficiency. As you will see, objects have many features and attributes that require Java to treat them differently than it treats the primitive types. By not applying the same overhead to the primitive types that applies to objects, Java can implement the primitive types more efficiently. Later, we will see object versions of the primitive types that are available for  use in those situations in which complete objects of these types are needed.

It is important to understand that new allocates memory for an object during run time. The advantage of this approach is that the program can create as many or as few objects as it needs during the execution of your program. However, since memory is finite, it is possible that new will not be able to allocate memory for an object because insufficient memory exists. If this happens, a run-time exception will occur.

The this Keyword

Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this keyword. It can be use inside any method to refer to the current object. That is, this is always a reference to the object on which the method was invoked. We can use this anywhere a reference to an object of the current class’ type is permitted.

To better understand what this refers to, consider the following version of Box( ):

// A redundant use of this
Box(double w, double h, double d) {
this.weight = w;
this.hight = h;
this.depth = d;
}

The use of this is redundant, but perfectly correct. Inside Box( ), this will always refer to the invoking object. While it is redundant in this case, this is useful in other contexts.

The use of this in such a context can sometimes be confusing, and some programmers are careful not to use local variables and formal parameter names that hide instance variables. Of course, other programmers believe the contrary—that it is a good convention to use the same names for clarity, and use this to overcome the instance variable hiding. It is a matter of taste which approach you adopt.

The static keyword

You have already seen the keyword static in front of the names of methods in some Java classes. A word such as this (as well as the words public and private) is called a modifier. A modifier determines the particular way a class, attribute or method is accessed. Let’s explore what this static modifier does.

Consider the BankAccount class. Say we wanted to have method which added interest, at the current rate, to the customer’s balance. It would be useful to have an attribute called interestRate to hold the value of the current rate of interest. But of course, the interest rate is the same for any customer—and if it changes, we want it to change for every customer in the bank; in other words for every object of the class. We can achieve this by declaring the variable as static.

An attribute declared as static is a class attribute; any changes made to it are made to all the objects in the class. The way this is achieved is by the program creating only one copy of the attribute and making it accessible to all objects. It would make sense if there were a way to access this attribute without reference to a specific object, and so there is! All we have to do is to declare methods such as setInterestRate and getInterestRate as static. This makes a
method into a class method; it does not refer to any specific object.

 

CONNECT WITH THE AUTHOR ON LINKEDIN.

YOU CAN VISIT OUR WEBSITE BRIGHTERBEES FOR INFORMATION ABOUT JAVA.
TO GET ON THE BLOG OF DAY 7 OF THIS JAVA SERIES click here.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US