Share variable between methods in Java -


i'm new in java , have little problem. want make array in 1 method , display in length in other. know how to both in 1 method:

class test {     public void create()     {         scanner in = new scanner(system.in);         system.out.println("number of elements: ");         int n=in.nextint();         int arr[]=new int[n];         system.out.println("number of elements: " + arr.length);     } } 

but how can this?

class test {     public void create()     {         scanner in = new scanner(system.in);         system.out.println("number of elements: ");         int n=in.nextint();         int arr[]=new int[n];     }      public void display()     {         system.out.println("wielkosc tablicy: " + arr.length);     } } 

make arr private instance variable of class, accessible method:

class test {     private int[] arr;     public void create()     {         scanner in = new scanner(system.in);         system.out.println("number of elements: ");         int n=in.nextint();         arr=new int[n];     }      public void display()     {         system.out.println("wielkosc tablicy: " + arr.length);     } } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -