7

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?

Pshemo
  • 122,468
  • 25
  • 185
  • 269

7 Answers7

14

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]);

    }
}
fr1550n
  • 1,055
  • 12
  • 23
  • This works but I am not sure why, so using the new creates an instance variable to be referenced from a non static enviornment? –  Jun 08 '13 at 22:05
  • 1
    The method arrPrint is defined as public void arrPrint(int[]) so it is "non-static" meaning that it can't be called unless it is called on an instance of Test. If you declared your method: public static void arrPrint(int[]) then you could call: Test.arrPrint(arr); within main without having created an instance for it. – fr1550n Jun 08 '13 at 22:18
2

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() {
    }
}
kaktooss
  • 41
  • 3
0

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.

Paul Sullivan
  • 2,865
  • 2
  • 19
  • 25
  • 1
    So how do I test my code then? I want to try and print out some code, do I get rid of main? When I do it tells me that I need a main. –  Jun 08 '13 at 21:48
0

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);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • not answering the question – Paul Sullivan Jun 08 '13 at 21:33
  • @PaulSullivan OP asked `how would I ever utilize a non static method inside of a main?`. How does my example not answering that question? – Pshemo Jun 08 '13 at 21:35
  • read the whole question – Paul Sullivan Jun 08 '13 at 21:37
  • i.e. `So if that is true how would I ever utilize a non static method inside of a main?` – Paul Sullivan Jun 08 '13 at 21:37
  • 2
    @PaulSullivan and which part Is not answered here? I am not saying that you are wrong, but I simply don't see what is missing in my answer. – Pshemo Jun 08 '13 at 21:39
  • basically you do not explicitly state that main is a special case (i.e. entry point). Your answer (and mine) needs expansion. What you have said is correct BUT doesnt atually answer why main is a special case – Paul Sullivan Jun 08 '13 at 21:42
  • let me reiterate the ops question in more technical terms: How is the object that the function call `main` is declared in not able to reference instance level functions and variables? how is it created? why is it different?` ... point made? I think so – Paul Sullivan Jun 08 '13 at 21:45
  • I believe you might overestimate what OP really wants. It is true that it OP don't understand idea behind `static` members and instance members, but to understand that he would need to read some good tutorial. Question as it is now is not asking "why is that" but "how to solve it". – Pshemo Jun 08 '13 at 22:11
0

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

0

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

kaktooss
  • 41
  • 3
  • 1
    You don't have to post answer. You can [[edit](http://stackoverflow.com/posts/17004049/edit)] old one and inform OP about update. – Pshemo Jun 08 '13 at 22:01
0

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.

Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59