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
Post a Comment