SRL Archive - Java Help and Tutorials

static errors?


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?

solarwind

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?


Do you mean static variables?

lardmaster

2006-11-06 20:00:30

no, i didnt make any static variables, thats the thing, but it was saying something about calling a function in a static context!

solarwind

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!


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

And how do you get that cat under your name?

CamHart

2006-11-06 23:18:40

Yea, post the error…

moparisthebest

2006-12-13 15:20:20

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

2006-12-13 21:01:30

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

2006-12-13 23:18:19

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

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 :)


:O improper indentation :o

Or, you can just make everything static :tongue:

lardmaster

2006-12-24 18:55:26

hah, mopar caught with bad indentation!

moparisthebest

2007-01-12 17:27:45

From: ilyaostr
: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.