Non-static objects - i.e. anything declared without the
static
keyword - are part of the "body" of a class, and only exist within an instance of that class: each time you create an new instance, you create a new version of each of the non-static objects within it.
It's like a car: when you buy a new car, it comes with it's own glove box - and you don;t expect all the stuff from your last car to magically appear within it!
The old car is one instance of the class Car, the new car is a second: and they each have separate non-static glove boxes. To access the right box, you have to spcify which instance of a Car you are looking in:
Car myNewCar = new Car();
myNewCar.Glovebox.Items.ListToConsole();
Car myOldCar = myGarage.GetCar("the red one");
myNewCar.Glovebox.Items.AddRange(myOldCar.Glovebox.Items);
For static objects, you don't use an instance, you use teh class name:
Console.WriteLine("A car has {0} wheels", Car.GetWheelsCount());
If you are in a static method, you can't access instance objects, because there is no instance specified for a static method - they work without any instance at all.
In your example,
main
is a static method - as it has to be - but
change
is not - it's an instance method, and requires an intance of the class to work.
Change it to this:
public static void change(int arr[]){
for(int counter=0;counter<arr.length;counter++){
arr[counter]+=5;
System.out.println(arr);
}
}
And it'll work.