
-----------------------------------
NiteIce
Mon Jan 16, 2006 8:52 pm

methods and variables
-----------------------------------
is it possible to use variables from one method in another method within the same class?

-----------------------------------
JackTruong
Mon Jan 16, 2006 8:56 pm


-----------------------------------
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

-----------------------------------
NiteIce
Mon Jan 16, 2006 8:59 pm


-----------------------------------
thanks, ill look into those

-----------------------------------
wtd
Mon Jan 16, 2006 9:08 pm


-----------------------------------
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?

-----------------------------------
NiteIce
Mon Jan 16, 2006 9:19 pm


-----------------------------------
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...)

-----------------------------------
NiteIce
Mon Jan 16, 2006 9:31 pm


-----------------------------------
will declaring the variable as static at the beginning of the class (before methods) work?

-----------------------------------
Hikaru79
Mon Jan 16, 2006 9:37 pm


-----------------------------------
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.

-----------------------------------
wtd
Mon Jan 16, 2006 9:50 pm


-----------------------------------
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.

-----------------------------------
NiteIce
Mon Jan 16, 2006 10:00 pm


-----------------------------------
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  :(

-----------------------------------
wtd
Mon Jan 16, 2006 10:26 pm


-----------------------------------
Care to show us code?

-----------------------------------
NiteIce
Mon Jan 16, 2006 10:33 pm


-----------------------------------
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):


int determine = (int) (Math.random () * 30) + 1;
        if (determine == 1)
        {
            c.println (TF1);
            TFAns = c.getChar ();
            if (TFAns == 't' | TFAns == 'T')
            {
                Placement ();
                playerTurn ();
                if (turn == 'x' | turn == 'X')
                {
                    marker [i] [j] = TTTArray [i] [j];
                }
                if (turn == 'f' | turn == 'F')
                {
                    marker [i] [j] = TTTArray [i] [j];
                }
            }
            if (TFAns == 'f' | TFAns == 'F')
            {
                c.println ("Sorry that is not the correct answer.");
            }
        }


 public static void Placement ()
    {
        char[] [] TTTArray = new char [3] [3];
        boolean questionAns;
        if (questionAns = true)
        {
            int x = 0;
            char markerpoint[] [] = new char [3] [3];

            while (x < 1)
            {
                c.println ("Choose where you would like to place your marker horizontally 0-2. (Must type in the same way as shown)");
                char i = c.readChar ();
                c.println ("Choose where you would like to place your marker vertically 0-2. (Must type in the same way as shown)");
                char j = c.readChar ();

                if (markerpoint [i] [j] != TTTArray [i] [j])
                {
                    TTTArray [i] [j] = markerpoint [i] [j];
                    x = x + 1;
                }
                if (markerpoint [i] [j] == TTTArray [i] [j])
                {
                    c.println ("Please choose another space, that one is already taken.");
                }
                else
                {
                    c.println ("You did not type in a number between 0 and 2. Please type where you would like to place your marker again.");
                }
            }
        }
    }

-----------------------------------
wtd
Tue Jan 17, 2006 12:03 am


-----------------------------------
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"?

-----------------------------------
NiteIce
Tue Jan 17, 2006 7:10 pm


-----------------------------------
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)

-----------------------------------
Martin
Tue Jan 17, 2006 8:26 pm


-----------------------------------
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?!
