This discussion is archived
8 Replies Latest reply: Jan 13, 2007 3:07 PM by 800282 RSS

"static context" problem

807599 Newbie
Currently Being Moderated
The numberGenerator class generates a random number and sends it to the Game class. This code works fine alone but JCreator reports a "non-static variable this cannot be referenced from a static context
numberGenerator bob = new numberGenerator();" problem when I try
^
to compile the entire program. What's the problem?



class Game{
     public static void main(String[] args){
// ...


               
     numberGenerator bob = new numberGenerator();
     int randomNumber X= bob.randomNumber();     
          
          

//...          
     }
}


class numberGenerator{
     
int randomNumber(){
          
//generate theNumber

     return theNumber;     
          
     }          
}
  • 1. Re: "static context" problem
    800282 Newbie
    Currently Being Moderated
    No problems here:
    class Game{
        public static void main(String[] args){
            numberGenerator bob = new numberGenerator();
            int randomNumberX = bob.randomNumber();
            System.out.println(randomNumberX);
        }
    }
    
    class numberGenerator{
        int randomNumber(){
            return 666;
        }
    }
  • 2. Re: "static context" problem
    807599 Newbie
    Currently Being Moderated
    class numberGenerator{
        int randomNumber(){
            return 666;
        }
    }
    ?!
  • 3. Re: "static context" problem
    800282 Newbie
    Currently Being Moderated
    But you're probably doing something like this:
    class Game{
        int aNumber
        public static void main(String[] args){
            // ...
            int anotherNumber = aNumber+1;
        }
    }
    which will give a compile error "cannot make static reference..."
  • 4. Re: "static context" problem
    800282 Newbie
    Currently Being Moderated
    class numberGenerator{
    int randomNumber(){
    return 666;
    }
    }
    ?!
    Ok ok, I'll adjust it:
    class numberGenerator{
        int pseudoRandomNumber(){
            return 666;
        }
    }
    ; )
  • 5. Re: "static context" problem
    800282 Newbie
    Currently Being Moderated
    @OP: You might want to read this:
    http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html
  • 6. Re: "static context" problem
    807599 Newbie
    Currently Being Moderated
    Ok ok, I'll adjust it:
    That's more like it. ;-)
  • 7. Re: "static context" problem
    807599 Newbie
    Currently Being Moderated
    Here's the whole program. Can anyone figure out how to get the generator to communicate with the main() class?

    //the purpose of this program is to create a 3X6 array of random letters and copy it to a
    //second array out of order. this creates 18 pairs of random letters that can later
    //be combined into one array.

    import java.util.*;

    class CreateRandomLetterCards
    {
    public static void main(String[] args)
    {
    String[][]CardsArray1 = new String[3][6]; //initialize two 3X6 arrays
    String[][]CardsArray2 = new String[3][6];
    String[][]FinalCardsArray = new String[6][6]; //final 6X6 array, not needed for now

    //*--------------------------------------------------------------------

    for(int i=0; i<3; i++) //create first array
    {
    for(int j=0; j<6; j++)
    {
    double b = 0; //assign first random letter
    while( !(b>65 && b<90))
    {
    b = Math.random()*100;
    }

    double d = 0; //assign second random letter
    while( !(d>65 & d<90))
    {
    d = Math.random()*100;
    }

    int randomNumber1 = (int)b;
    int randomNumber2 = (int)d;

    char x = (char)randomNumber1;
    char y = (char)randomNumber2;

    StringBuffer buffer = new StringBuffer();
    buffer.append(x).append(y);
    String s = buffer.toString(); //combine two letters

    CardsArray1[i][j]=s;

    }
    } //end create first array


    //*--------------------------------------------------------------------

    for (int g=0; g<3; g++) //create second array
    {                                                                       //(copy of first array out of order)


    boolean placeHolder0 = true;
    boolean placeHolder1 = true;
    boolean placeHolder2 = true;
    boolean placeHolder3 = true;
    boolean placeHolder4 = true;
    boolean placeHolder5 = true;


    for (int h=0; h<6; h++)
    {

                                  numberGenerator bob = new numberGenerator();
                                            int randomNumberX = bob.randomNumber();
                             
                             





    if(randomNumberX==0 && placeHolder0)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder0=false;
    }

    else if(randomNumberX==1 && placeHolder1)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder1=false;
    }

    else if(randomNumberX==2 && placeHolder2)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder2=false;
    }

    else if(randomNumberX==3 && placeHolder3)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder3=false;
    }

    else if(randomNumberX==4 && placeHolder4)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder4=false;
    }

    else if(randomNumberX==5 && placeHolder5)
    {
    CardsArray2[g][randomNumberX] = CardsArray1[g][h];
    placeHolder5=false;
    }

                        else
                        {
                                  h = h-1;     
                                  
                        }
                   
                   }




    }     //end create second array




    //*--------------------------------------------------------------------
    int bob = 0;
    for(int BOND=0; BOND<3; BOND++){                //test: print arrays
    for(int SCARFACE=0; SCARFACE<6; SCARFACE++){
    System.out.println(bob);
    System.out.println(CardsArray1[BOND][SCARFACE]);
    System.out.println(CardsArray2[BOND][SCARFACE]);
    System.out.println();
    bob++;
              }
                   }





    } //end main

    //*--------------------------------------------------------------------


    class numberGenerator{
         
         int randomNumber(){
                   double theNumberDouble = -1;
              int theNumber = -1;
              
              while(!(theNumber>=0 && theNumber<6))
    {
    theNumberDouble = Math.random()*10;
    theNumber = (int)theNumberDouble;
              }

              
              
                   return theNumber;     
              
              
         }          
    }     

    //*--------------------------------------------------------------------

    }
  • 8. Re: "static context" problem
    807599 Newbie
    Currently Being Moderated
    I looks like you are defining the number Generator class inside the Game class.
    class Game{
        public static void main(String[] args){
            numberGenerator bob = new numberGenerator();
        }
    
        class numberGenerator{
    
            int randomNumber(){
                int theNumber=4;
                return theNumber; 
            } 
        }
    }
    That won't work. The class numberGenerator is a non-static member of the class Game. Therefore the static method main can't access it.
    You either have to move the numberGenerator class outside the Game class or define it static.

    e.g.
    class Game{
        public static void main(String[] args){
            numberGenerator bob = new numberGenerator();
        }
    
        static class numberGenerator{
    
            int randomNumber(){
                int theNumber=4;
                return theNumber; 
            } 
        }
    }