0 votes
in JAVA by
What do you understand by an instance variable and a local variable?

1 Answer

0 votes
by
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.

Example:

class Athlete {

public String athleteName;

public double athleteSpeed;

public int athleteAge;

}

Local variables are those variables present within a block, function, or a 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.

Example:

public void athlete() {

String athleteName;

double athleteSpeed;

int athleteAge;

}

Related questions

+1 vote
asked May 5, 2021 in JAVA by SakshiSharma
+1 vote
asked May 13, 2021 in JAVA by rajeshsharma
...