0% found this document useful (0 votes)
50 views13 pages

Oops Presentation

Uploaded by

shuvampal23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views13 pages

Oops Presentation

Uploaded by

shuvampal23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Object Oriented

Programming
Presentation
By
Group 16
Shuvam Pal - 11500221052
Hrishikesh Kumar Chaudhary - 11500221054
Ishika Rana - 11500221055
Dipanjan Saha - 11500221056
Use of this and static
keywords
INTRODUCTION
In the Java programming language, a keyword is any one of 67
reserved words that have a predefined meaning in the language.
Because of this, programmers cannot use keywords in some
contexts, such as names for variables, methods, classes, or as any
other identifier. Of these 67 keywords, 16 of them are only
contextually reserved, and can sometimes be used as an identifier,
unlike standard reserved words. Due to their special functions in the
language, most integrated development environments for Java use
syntax highlighting to display keywords in a different colour for
easy identification.
this keyword
The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class
attributes and parameters with the same name (because a class attribute is shadowed
by a method or constructor parameter).
If you omit the keyword in the example above, the output would be "0" instead of "5".
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.
1) 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. For instance, when parameters (formal arguments) and
instance variables are same, we use this keyword to distinguish local variable and
instance variable.

2) this: to invoke current


class method
We may invoke the method of the current class by using the this keyword.
If we don't use the this keyword, compiler automatically adds this keyword
while invoking the method.
3) this() : to invoke current class
constructor
The this() constructor call should be used to reuse the constructor from the constructor. It
maintains the chain between the constructors i.e. it is used for constructor chaining.

4) this: to pass as an argument in


the method
The this keyword can also be passed as an argument in the method. It is mainly used in
the event handling.
5) this: to pass as argument in the
constructor call
We can pass the this keyword in the constructor also. It is useful if we have to use one object
in multiple classes.

6) this keyword can be used to


return current class instance
We can return this keyword as a statement from the method. In such case, return type of
the method must be the class type (non-primitive).
static keyword
The static keyword in Java is used for memory management mainly. We can apply
static keyword with variables, methods, blocks and nested classes. The static
keyword belongs to the class than an instance of the class.
The static can be:
1. Variable (also known as a class variable)

2. Method (also known as a class method)

3. Block

4. Nested class
1) Java 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 of static variable - It makes your program memory efficient (i.e., it saves memory).
2) Java static method
If you apply static keyword with any method, it is known as static method .
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a class.
• A static method can access static data member and can change the value of it.

3) Java static block


• Is used to initialize the static data member.
• It is executed before the main method at the time of class loading.
CONCLUSION

Advantages of using ‘this’ keyword:

There are certain advantages of using ‘this’ reference in Java as mentioned below:
1. It helps to distinguish between instance variables and local variables with the same
name.
2. It can be used to pass the current object as an argument to another method.
3. It can be used to return the current object from a method.
4. It can be used to invoke a constructor from another overloaded constructor in the same
class.

Disadvantages of using ‘this’ keyword:

Although ‘this’ reference comes with many advantages there are certain disadvantages of
also:
1. Overuse of this can make the code harder to read and understand.
2. Using this unnecessarily can add unnecessary overhead to the program.
3. Using this in a static context results in a compile-time error.
ACKNOWLEDGMENT
Our sincere thanks to Miss Swagata Kundu who gave us
the golden opportunity to do our term paper on this
wonderful topic on Mathematics and has continuously
supported us in completing our term paper. We came to
know about so many new things and are very much
thankful to her. We also like to thank our institute for
making us a part of their system.
Secondly , I would also like to thank my team members
who helped me a lot in finalizing this project and getting
references in the limited time frame.

You might also like