1

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?

Atinuke
  • 191
  • 2
  • 4
  • 11
  • The term 'non-local variable' is not typically used in Java. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html gives you an outline of what's what. – pvg Mar 07 '17 at 04:59

3 Answers3

2

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

  1. Instance Variables (Non-Static Fields)
  2. Class Variables (Static Fields)
  3. Parameters
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
1

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);      

    }



}
Jackson Baby
  • 398
  • 3
  • 4
  • 20
0

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.

Falla Coulibaly
  • 759
  • 2
  • 11
  • 23