Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 methods and variables
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
NiteIce




PostPosted: 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?
Sponsor
Sponsor
Sponsor
sponsor
JackTruong




PostPosted: Mon Jan 16, 2006 8:56 pm   Post subject: (No 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
NiteIce




PostPosted: Mon Jan 16, 2006 8:59 pm   Post subject: (No subject)

thanks, ill look into those
wtd




PostPosted: Mon Jan 16, 2006 9:08 pm   Post subject: (No 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?
NiteIce




PostPosted: Mon Jan 16, 2006 9:19 pm   Post subject: (No 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...)
NiteIce




PostPosted: Mon Jan 16, 2006 9:31 pm   Post subject: (No subject)

will declaring the variable as static at the beginning of the class (before methods) work?
Hikaru79




PostPosted: Mon Jan 16, 2006 9:37 pm   Post subject: (No 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.
wtd




PostPosted: Mon Jan 16, 2006 9:50 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
NiteIce




PostPosted: Mon Jan 16, 2006 10:00 pm   Post subject: (No 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 Sad
wtd




PostPosted: Mon Jan 16, 2006 10:26 pm   Post subject: (No subject)

Care to show us code?
NiteIce




PostPosted: Mon Jan 16, 2006 10:33 pm   Post subject: (No 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):


code:
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.");
            }
        }



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




PostPosted: Tue Jan 17, 2006 12:03 am   Post subject: (No 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"?
NiteIce




PostPosted: Tue Jan 17, 2006 7:10 pm   Post subject: (No 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)
Martin




PostPosted: Tue Jan 17, 2006 8:26 pm   Post subject: (No 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?!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: