Difference between instance variable and local variable | Java 8 Interview Questions 2021 - J03

preview_player
Показать описание
Difference between instance variable and local variable | Java 8 Interview Questions 2021 - J03

What is the difference between an instance variable and a local variable?
Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.
All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.
Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.
In the following example, we have a class, Employee where id, name and specialty will be treated as instance variables once an object is created where as v1,v2 and v3 will be local variables
Рекомендации по теме