2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by stultuske
0

can anyone explain with me briefly of instance variables and local variables.

any basic course of java would do that ...

instance variables are variables that you store in Objects, and can modify or use, by using the setters and getters.
local variables are used locally only, they can only be "seen" by the method in which they're created and used

public class MyClass{
private int instanceVar;
public MyClass(int instance){
int localVar = 5;
if ( instance >= localVar)
setInstanceVar(instance);
else
setInstanceVar(localVar);
}

public void setInstanceVar(int instance){
this.instanceVar = instance;
}
public int getInstanceVar(){
return this.instanceVar;
}
}

in here, instanceVar is an instance variable, it can be modified or the value can be asked by any class creating and instanciating a MyClass instance.
localVar is a local variable, it can only be read within the constructor.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.