|
Author |
Topic: little dumb question (Read 275 times) |
|
lunetta
|
little dumb question
« on: Apr 25th, 2004, 8:24pm » |
|
int a = 1; void setup() { a = 2; a = 3; } a = 4; void loop() { a = 5; println (a); } this simple example does not work; why can't I reassign a global variable outside the voids? It's not a truly necessary thing, I was just wondering the reason...
|
|
|
|
fry
|
Re: little dumb question
« Reply #1 on: Apr 25th, 2004, 11:23pm » |
|
because there's no determination for when the 'a=4' line would be run. is it when the program is first created? is it before setup()? java, and therefore processing, programs are built around methods (like setup/loop/etc) so everything must be inside a method. the difference is that in 'static' mode, where there are no methods at all, processing cheats and puts your entire program inside 'setup' behind the scenes. so this program: int a = 1; println(a); becomes: void setup() { int a = 1; println(a); } when you hit the 'run' button. on the other hand, the difference with the 'int a = 1' at the beginning, is that this has a specific timing--that when the program is first created, a slot for something called 'a' will be created, and it will initially be filled with '1'. that make sense?
|
|
|
|
mflux
|
Re: little dumb question
« Reply #2 on: Apr 26th, 2004, 12:02am » |
|
Hi Ben That makes sense, but what about declaring and initializing an instance of an object? Ball a=new Ball(); class Ball { Ball() { } } void setup() { } void loop() { } This would not work, even though it's much like making a new variable and intializing it. I'm forced to do: Ball a; class Ball { Ball() { } } void setup() { a=new Ball(); } void loop() { }
|
« Last Edit: Apr 26th, 2004, 12:03am by mflux » |
|
|
|
|
fry
|
Re: little dumb question
« Reply #3 on: Apr 26th, 2004, 5:14am » |
|
that first program runs just fine for me, as it should. if you're getting a problem with that type of thing, it's more likely to be in the preprocessor or compiler (and a bug), because as you point out, it should work.
|
|
|
|
lunetta
|
Re: little dumb question
« Reply #4 on: Apr 26th, 2004, 5:42am » |
|
thanks guys for the attention; this wasn't a problem I'm experiencing, but something I wanted to understand. It's clear now, thanks!
|
|
|
|
|