PDA

View Full Version : static errors?



lardmaster
11-07-2006, 12:02 AM
what happens when you get an error about static stuff if you call a non-static sub inside your main? why would that happen?

solarwind
11-07-2006, 12:37 AM
what happens when you get an error about static stuff if you call a non-static sub inside your main? why would that happen?

Do you mean static variables?

lardmaster
11-07-2006, 01:00 AM
no, i didnt make any static variables, thats the thing, but it was saying something about calling a function in a static context!

solarwind
11-07-2006, 01:05 AM
no, i didnt make any static variables, thats the thing, but it was saying something about calling a function in a static context!

Can you post the code please? I'll try helping you.

And how do you get that cat under your name?

CamHart
11-07-2006, 04:18 AM
Yea, post the error...

moparisthebest
12-13-2006, 08:20 PM
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 :)

lardmaster
12-14-2006, 02:01 AM
thank you, i realized i did what you said, i forgot to make a static procedure static, because it was in my main class... thanks for all the help.

solarwind: you get it for being a scryptor!

solarwind
12-14-2006, 04:18 AM
Cool, glad you got it. If you want a good, detailed explanation on why you got that error and other useful stuff, read my Java Modifier tutorial over at Impsoft.

ilyaostr
12-24-2006, 05:11 PM
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 :)

:O improper indentation :o

Or, you can just make everything static :tongue:

lardmaster
12-24-2006, 11:55 PM
hah, mopar caught with bad indentation!

moparisthebest
01-12-2007, 10:27 PM
:O improper indentation :o

Or, you can just make everything static :tongue:

Well I typed it up in this little box, oh well :p

You can't make everything static all the time, in fact it is bad coding practice to do so if it is not needed.