Oops Presentation
Oops Presentation
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.
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.
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.
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.