Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
package arraysınmethods;


public class ArraysInMethods {

    
    public static void main(String[] args) {
        int bucky[] = {3,4,5,6,7};
        change(bucky);
        for(int x:bucky)
            System.out.println(x);
    }
    public void change(int arr[]){
        for(int counter=0;counter<arr.length;counter++){
            arr[counter]+=5;
            System.out.println(arr);
        }
    }
}


What I have tried:

Why i get this error?"non-static method change() cannot be referenced from a static context".
And output is:
[I@4554617c
[I@4554617c
[I@4554617c
[I@4554617c
[I@4554617c
8
9
10
11
12

And after i changed this metod to be static,why it gives something like this ->[I@4554617c ?
Posted
Updated 7-Jul-17 1:51am
Comments
Prifti Constantine 7-Jul-17 7:29am    
Thats because on the prin youre printing out the ARRAY and probably thats the refference location of the array. In order to prin out the value of the array you also have to specify the Index of the array in order to get the value that is stored inside. System.out.println(arr[index]);
Member 12702056 7-Jul-17 7:58am    
Thanks a lot,i got it :)

1 solution

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:
C#
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:
C#
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.
 
Share this answer
 
Comments
Member 12702056 7-Jul-17 8:00am    
This means for calling some methods inside o another static method,those methods also must be static,could i get it?is that right?
OriginalGriff 7-Jul-17 8:22am    
Yes - unless you create (or access) an instance of the class.
Member 12702056 7-Jul-17 8:28am    
Thank you very much :)
Member 12702056 7-Jul-17 8:31am    
With creating object of another class,we use that object to call methods of that class and it must not be static. Without being static with using object dot method name and paranthesis in main method of main class, we can call method of that class as you showed above. I got it. Thanks again :)
OriginalGriff 7-Jul-17 8:39am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900