lardmaster
2006-11-06 19:02:07
what happens when you get an error about static stuff if you call a non-static sub inside your main? why would that happen?
2006-11-06 19:02:07
2006-11-06 19:37:05
From: lardmaster
what happens when you get an error about static stuff if you call a non-static sub inside your main? why would that happen?
2006-11-06 20:00:30
2006-11-06 20:05:18
From: lardmaster
no, i didnt make any static variables, thats the thing, but it was saying something about calling a function in a static context!
2006-11-06 23:18:40
2006-12-13 15:20:20
public class Bob{
public static void main(String[] args){
//the line below is not allowed because that function is not static
nonStaticFunction();
//if you want to call it you must do something like this:
Bob bob = new Bob();
bob.nonStaticFunction();
//or if you only want to use that instance once (generallly frowned upon)
new Bob().nonStaticFunction();
}
public void nonStaticFunction(){
System.out.println("this can only be called from an instance of Bob");
}
}
2006-12-13 21:01:30
2006-12-13 23:18:19
2006-12-24 12:11:35
From: moparisthebest
I assume that the error you are getting means that you cannot call a non static function or field from a static context. I will try to explain it. What that means is this:
public class Bob{
public static void main(String[] args){
//the line below is not allowed because that function is not static
nonStaticFunction();
//if you want to call it you must do something like this:
Bob bob = new Bob();
bob.nonStaticFunction();
//or if you only want to use that instance once (generallly frowned upon)
new Bob().nonStaticFunction();
}
public void nonStaticFunction(){
System.out.println("this can only be called from an instance of Bob");
}
}
I hope that clears it up for you, it is all because of OOP (Object Oriented Programming)
If you need more help just post :)
2006-12-24 18:55:26
2007-01-12 17:27:45
From: ilyaostr
:O improper indentation :o
Or, you can just make everything static :tongue: