Computer Science Canada methods and variables |
Author: | NiteIce [ Mon Jan 16, 2006 8:52 pm ] |
Post subject: | methods and variables |
is it possible to use variables from one method in another method within the same class? |
Author: | JackTruong [ Mon Jan 16, 2006 8:56 pm ] |
Post subject: | |
No. If variables were created in a method, it is a local method and can only be accessed within that method. Some work arounds: Global variables Parameter passing |
Author: | NiteIce [ Mon Jan 16, 2006 8:59 pm ] |
Post subject: | |
thanks, ill look into those |
Author: | wtd [ Mon Jan 16, 2006 9:08 pm ] |
Post subject: | |
JackTruong wrote: No. If variables were created in a method, it is a local method and can only be accessed within that method.
Some work arounds: Global variables There are no global variables. Perhaps you mean instance or class-scoped variables? |
Author: | NiteIce [ Mon Jan 16, 2006 9:19 pm ] |
Post subject: | |
well, i read the tutorial on parameters, and i tried to use them for my program, but I often get the errors: no overload available for method() and another one when i try returning an array char to use in another method (i am very lost with what i am doing...) |
Author: | NiteIce [ Mon Jan 16, 2006 9:31 pm ] |
Post subject: | |
will declaring the variable as static at the beginning of the class (before methods) work? |
Author: | Hikaru79 [ Mon Jan 16, 2006 9:37 pm ] |
Post subject: | |
NiteIce wrote: will declaring the variable as static at the beginning of the class (before methods) work?
Sort of yes, but not in the way you want it to. Stay away from 'static' variables until you do more with Java's object and class system. Then you'll understand what 'static' really means. |
Author: | wtd [ Mon Jan 16, 2006 9:50 pm ] |
Post subject: | |
Declaring everything as static is kinda of like buying a car with a manual transmission, then realizing you can't drive stick shift, and "fixing" that by only ever driving in first gear. |
Author: | NiteIce [ Mon Jan 16, 2006 10:00 pm ] |
Post subject: | |
well, im not declaring everything as static.. just the ones i need to use in 2 or 3 different methods.. i just cant find any other way to get this too work.. ive never had to make a program like this before |
Author: | wtd [ Mon Jan 16, 2006 10:26 pm ] |
Post subject: | |
Care to show us code? |
Author: | NiteIce [ Mon Jan 16, 2006 10:33 pm ] | ||||
Post subject: | |||||
hmm... lets see.. ill show u a sample from one method and another method i have (note that i am using Ready and hsa console):
|
Author: | wtd [ Tue Jan 17, 2006 12:03 am ] |
Post subject: | |
Solve the style things first... variables and method names should always begin with a lowercase letter. Additionally, they should be meaningful. What the heck is "TFAns"? |
Author: | NiteIce [ Tue Jan 17, 2006 7:10 pm ] |
Post subject: | |
TFAns was for True/False answer (the program is a trivia tic tac toe, where you can only choose a space to place your marker if you get the true/false answer correct) |
Author: | Martin [ Tue Jan 17, 2006 8:26 pm ] |
Post subject: | |
wtd wrote: Declaring everything as static is kinda of like buying a car with a manual transmission, then realizing you can't drive stick shift, and "fixing" that by only ever driving in first gear.
From the movie Dogma: (Yelling over the engine) What gear are you in? Gears?! |