Java usage of keywords

Ace your homework & exams now with Quizwiz!

Static variable

If you declare any variable as static, it is known as a static variable. The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading

Static Keyword

The static keyword in Java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than an instance of the class. The static can be: Variable (also known as a class variable) Method (also known as a class method) Block Nested class

Using "super" to invoke parent class method

The super keyword can also be used to invoke parent class method. It should be used if subclass contains the same method as parent class. In other words, it is used if method is overridden.

Using "super" to invoke parent class constructor

The super keyword can also be used to invoke the parent class constructor.

Usage of "super" keyword

The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable. super can be used to refer immediate parent class instance variable. super can be used to invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.

"this" : to refer current class instance variable?

The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

"this" : to invoke current class constructor

The this() constructor call can be used to invoke the current class constructor. It is used to reuse the constructor. In other words, it is used for constructor chaining.

Usage of the "new" keyword

When you are declaring a class in java, you are just creating a new data type. A class provides the blueprint for objects. You can create an object from a class. Declaration : First, you must declare a variable of the class type. This variable does not define an object. Instantiation and Initialization : Second, you must acquire an actual, physical copy of the object and assign it to that variable. You can do this using the new operator. The new operator instantiates a class by dynamically allocating(i.e, allocation at run time) memory for a new object and returning a reference to that memory. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated. The new operator is also followed by a call to a class constructor, which initializes the new object. 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.

"this" : to invoke current class method

You may invoke the method of the current class by using the this keyword. If you don't use the this keyword, compiler automatically adds this keyword while invoking the method.

Usage of "this" keyword

In java, this is a reference variable that refers to the current object. Here is given the 6 usage of java this keyword. 1)this can be used to refer current class instance variable. 2)this can be used to invoke current class method (implicitly) 3)this() can be used to invoke current class constructor. 4)this can be passed as an argument in the method call. 5)this can be passed as argument in the constructor call. 6)this can be used to return the current class instance from the method.

static variable advantages?

It makes your program memory efficient (i.e., it saves memory).

Final Keyword

The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be: variable method class The final keyword can be applied with the variables, a final variable that have no value it is called blank final variable or uninitialized final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only.

Using "super" to refer parent class instance variable

We can use super keyword to access the data member or field of parent class. It is used if parent class and child class have same fields.


Related study sets

EMT- Chapter 38 (Vehicle extraction & special rescue)

View Set

(PrepU) Chapter 26: Health Assessment

View Set

(Last day part 2 ) Finals term 1

View Set

KORE 141 1과 인사 Lecture Notes

View Set

Auditing Principles Unit 3 Study Guide

View Set

Integumentary PrepU Health Assessment

View Set