-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThis.txt
More file actions
25 lines (18 loc) · 1.27 KB
/
This.txt
File metadata and controls
25 lines (18 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
* #1
Instance variable:- These variables are declared within a class but outside a method.
These variables are created when we create an object and are destroyed when the object is destroyed.
Local variable:- These variables are declared within a method but do not get any default value.
They are created when we enter a method or constructor and get destroyed when the call returns from the method.
#2
In Java, it is not allowed to declare two or more variables having the same name inside a scope either in class scope or method scope.
The preference is always given to the local variable.
Creating an object is not a good idea to solve the issue of conflicting names of instance and local variables.
We can differentiate between local and instance variables by passing an object itself.
Instead of writing and passing an object, we can simply use this keyword.
#3
This keyword:-
This keyword represents the current object.
Current object is an object which is calling the method.
This keyword is used to invoke the current class constructor.
Using the 'this' keyword, we can return the current class instance.
This keyword can be used as an argument in the constructor call.