How do I call a non static method from a main method? [duplicate]
Asked Answered
F

7

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?

Fluke answered 8/6, 2013 at 21:25 Comment(1)
see my answer and ask if any qsEcto
C
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]);

    }
}
Cinematography answered 8/6, 2013 at 22:4 Comment(2)
This works but I am not sure why, so using the new creates an instance variable to be referenced from a non static enviornment?Fluke
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.Cinematography
B
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() {
    }
}
Blastomere answered 8/6, 2013 at 21:30 Comment(3)
Not answering the questionEcto
Oh, believe me, it doesBlastomere
I did use new in my example now that I edited.Fluke
E
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.

Ecto answered 8/6, 2013 at 21:30 Comment(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.Fluke
P
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);
Pate answered 8/6, 2013 at 21:30 Comment(8)
not answering the questionEcto
@PaulSullivan OP asked how would I ever utilize a non static method inside of a main?. How does my example not answering that question?Pate
read the whole questionEcto
i.e. So if that is true how would I ever utilize a non static method inside of a main?Ecto
@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.Pate
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 caseEcto
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 soEcto
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".Pate
H
0

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

Humorist answered 8/6, 2013 at 21:31 Comment(0)
B
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

Blastomere answered 8/6, 2013 at 21:59 Comment(1)
You don't have to post answer. You can [edit] old one and inform OP about update.Pate
L
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.

Lie answered 8/6, 2013 at 22:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.