Question

For example, I am trying to do something like this

public class Test {

    public static void main(String args[]) {

        int[] arr = new int[5];

        arrPrint(arr);
    }

    public void arrPrint(int[] arr) {

        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
}

I get an error telling me that I can't reference non-static variables from static enviorments. So if that is true how would I ever utilize a non static method inside of a main?

Was it helpful?

Solution

You can't. A non-static method is one that must be called on an instance of your Test class; create an instance of Test to play with in your main method:

public class Test {

    public static void main(String args[]) {
        int[] arr = new int[5];
        arr = new int[] { 1, 2, 3, 4, 5 };

        Test test = new Test();
        test.arrPrint(arr);

    }

    public void arrPrint(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
}

OTHER TIPS

You can call non-static method only using a class instance, so you have to create it using new keyword.

public class Something {

    public static void main(String args[]) {
        Something something = new Something();
        something.method1();

        new Something().method2();
    }

    public void method1() {
    }

    public void method2() {
    }
}

new Something().method1() or new Something().method2()

In short you can't. As main is a special case (i.e. entry point of which there an only be one) you can't have anything other than static methods, variables in main.

As per your new example the solution will be:

public class Test {

    public static void main(String args[]) {
        int[] arr = new int[5];
        new Test().arrPrint(arr);
    }

    public void arrPrint(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);

    }
}

Or you can move

int[] arr = new int[5];

to the static section like

public class Test {

    static int[] arr; 

    public static void main(String args[]) {
        arr = new int[5]; 
        new Test().arrPrint(arr);
    }

    public void arrPrint(int[] arr) {
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }
}

But the second one smells really bad from point of good programming practices

Non static methods need to be invoked on instance of class. To create instance use new keyword like

Test instance = new Test();

now you will be able to invoke methods on instance like

instance.arrPrint(arr);

non-static -> property of the object

static method -> property of the class it-self.

So when there is no static keyword in a method/variable declaration you CAN NOT invoke/make reference to that method/variable without any instance of the class from a static context.

As everyone else suggested create a new instance(new Test()) of the main class in main method and invoke non-static arrPrintmethod.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top