I have the following code below:
public static void main(String args[])
{
start();
}
i get this error: Non-static method start() cannot be referenced from a static context.
How can i go about doing this?
Create an instance of your class and call the start method of that instance.
If your class is named Foo then use the following code in your main method:
Foo f = new Foo();
f.start();
Alternatively, make method start static, by declaring it as static.
Hope this can help you..
public class testProgarm {
private static void start() {
System.out.println("Test");
}
public static void main(String[] args) {
start();
}
}
However, it is not a good practice to make a method static. You should instantiate a object and call a object's method instead. If your object does't have a state, or you need to implement a helper method, static is the way to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With