pls, can someone explain with examples what "non-local" variables are in java?
My Understanding Non-local variables are object variables. But when called object variables would that be referring to the variables used in the object methods?
pls, can someone explain with examples what "non-local" variables are in java?
My Understanding Non-local variables are object variables. But when called object variables would that be referring to the variables used in the object methods?
In Java Programming language, there are 4 kinds of variables.
Local Variables : These are variables that are declared within method scope. A method will often store its temporary state in local variables.
If you ask for Non-Local variables, then you'd refer to all other variables but local; like
Instance variables(non-local) are declared in a class, but outside a method, constructor or any block.Instance variables belong to an instance of a class, Every object has it’s own copy of the instance variables
public class InstanceClassSample {
String name = "Java";
public void testName(){
//instanceClassSample and instanceClassSample2 will have it own copy of name
InstanceClassSample instanceClassSample = new InstanceClassSample();
InstanceClassSample instanceClassSample2 = new InstanceClassSample();
System.out.println(instanceClassSample.name);
System.out.println(instanceClassSample2.name);
}
}
A local variable will be declared within {
and }
of a method. Outside the the braces, the variable will no longer be accessible and garbage collected.
As far as I know object variable is not really a thing in Java, you can have an instance variable or a class variable, those would technically be your 'non-local' variables.