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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.