Usage of Keywords Java

¡Supera tus tareas y exámenes ahora con Quizwiz!

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.

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.

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.

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. Advantages It makes your program memory efficient (i.e., it saves memory).

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.

Usage of "super" keyword

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

"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.

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.

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.

"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.

There can be a lot of usage of java this keyword.

In java, this is a reference variable that refers to the current 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.

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 static can be:

Variable (also known as a class variable) Method (also known as a class method) Block Nested class

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.


Conjuntos de estudio relacionados

Research Methods in Psychology chapter 2

View Set

CIS 1010 Cyber Security Exam 2021 - HB Quizlet PDF

View Set

10 (CNTRCT): Formation of Sales and Lease Contracts

View Set

ECN 212 - CH 14 competitive firms

View Set

Mastering Biology HHMI: Got Lactase?

View Set

Abnormal Psych. Exam 3 Study Guide

View Set