non static method cannot be referenced from a static context [duplicate]











up vote
16
down vote

favorite
4













This question already has an answer here:




  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers




First some code:



import java.util.*;
//...

class TicTacToe
{
//...

public static void main (Stringarg)
{

Random Random = new Random() ;
toerunner () ; // this leads to a path of
// methods that eventualy gets us to the rest of the code
}
//...

public void CompTurn (int type, boolean debug)
{
//...

boolean done = true ;
int a = 0 ;
while (!done)
{
a = Random.nextInt(10) ;
if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }}
if (possibles[a]==1) done = true ;
}
this.board[a] = 2 ;


}
//...

} //to close the class


Here is the error message:



TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
a = Random.nextInt(10) ;
^


What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?










share|improve this question















marked as duplicate by diosney, zisoft, Laf, Shankar Damodaran, Tobrun Dec 12 '14 at 15:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Random Random = new Random() should be Random random = new Random()
    – JRL
    Apr 22 '10 at 21:21










  • See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
    – Mark Elliot
    Apr 22 '10 at 21:25















up vote
16
down vote

favorite
4













This question already has an answer here:




  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers




First some code:



import java.util.*;
//...

class TicTacToe
{
//...

public static void main (Stringarg)
{

Random Random = new Random() ;
toerunner () ; // this leads to a path of
// methods that eventualy gets us to the rest of the code
}
//...

public void CompTurn (int type, boolean debug)
{
//...

boolean done = true ;
int a = 0 ;
while (!done)
{
a = Random.nextInt(10) ;
if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }}
if (possibles[a]==1) done = true ;
}
this.board[a] = 2 ;


}
//...

} //to close the class


Here is the error message:



TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
a = Random.nextInt(10) ;
^


What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?










share|improve this question















marked as duplicate by diosney, zisoft, Laf, Shankar Damodaran, Tobrun Dec 12 '14 at 15:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    Random Random = new Random() should be Random random = new Random()
    – JRL
    Apr 22 '10 at 21:21










  • See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
    – Mark Elliot
    Apr 22 '10 at 21:25













up vote
16
down vote

favorite
4









up vote
16
down vote

favorite
4






4






This question already has an answer here:




  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers




First some code:



import java.util.*;
//...

class TicTacToe
{
//...

public static void main (Stringarg)
{

Random Random = new Random() ;
toerunner () ; // this leads to a path of
// methods that eventualy gets us to the rest of the code
}
//...

public void CompTurn (int type, boolean debug)
{
//...

boolean done = true ;
int a = 0 ;
while (!done)
{
a = Random.nextInt(10) ;
if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }}
if (possibles[a]==1) done = true ;
}
this.board[a] = 2 ;


}
//...

} //to close the class


Here is the error message:



TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
a = Random.nextInt(10) ;
^


What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?










share|improve this question
















This question already has an answer here:




  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers




First some code:



import java.util.*;
//...

class TicTacToe
{
//...

public static void main (Stringarg)
{

Random Random = new Random() ;
toerunner () ; // this leads to a path of
// methods that eventualy gets us to the rest of the code
}
//...

public void CompTurn (int type, boolean debug)
{
//...

boolean done = true ;
int a = 0 ;
while (!done)
{
a = Random.nextInt(10) ;
if (debug) { int i = 0 ; while (i<20) { System.out.print (a+", ") ; i++; }}
if (possibles[a]==1) done = true ;
}
this.board[a] = 2 ;


}
//...

} //to close the class


Here is the error message:



TicTacToe.java:85: non-static method nextInt(int) cannot be referenced from a static context
a = Random.nextInt(10) ;
^


What exactly went wrong? What does that error message "non static method cannot be referenced from a static context" mean?





This question already has an answer here:




  • What is the reason behind “non-static method cannot be referenced from a static context”? [duplicate]

    13 answers








java static






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 22 '10 at 21:27









Mark Elliot

57k15122150




57k15122150










asked Apr 22 '10 at 21:16









David

5,681256291




5,681256291




marked as duplicate by diosney, zisoft, Laf, Shankar Damodaran, Tobrun Dec 12 '14 at 15:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by diosney, zisoft, Laf, Shankar Damodaran, Tobrun Dec 12 '14 at 15:30


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    Random Random = new Random() should be Random random = new Random()
    – JRL
    Apr 22 '10 at 21:21










  • See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
    – Mark Elliot
    Apr 22 '10 at 21:25














  • 2




    Random Random = new Random() should be Random random = new Random()
    – JRL
    Apr 22 '10 at 21:21










  • See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
    – Mark Elliot
    Apr 22 '10 at 21:25








2




2




Random Random = new Random() should be Random random = new Random()
– JRL
Apr 22 '10 at 21:21




Random Random = new Random() should be Random random = new Random()
– JRL
Apr 22 '10 at 21:21












See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
– Mark Elliot
Apr 22 '10 at 21:25




See also stackoverflow.com/questions/2694470/whats-wrong-with-my-random
– Mark Elliot
Apr 22 '10 at 21:25












4 Answers
4






active

oldest

votes

















up vote
27
down vote



accepted










You are calling nextInt statically by using Random.nextInt.



Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).



It would be definitely worth while to check out:




  • What is the reason behind "non staticmethod cannot be referenced from a static context"?


Update:



You really should replace this line,



Random Random = new Random(); 


with something like this,



Random r = new Random();


If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.






share|improve this answer























  • then i get a cannot find symbol error in regard to the r.
    – David
    Apr 22 '10 at 21:19










  • Did you initialize r? It can't just appear out of nowhere.
    – Anthony Forloney
    Apr 22 '10 at 21:20










  • yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
    – David
    Apr 22 '10 at 21:22










  • Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
    – Anthony Forloney
    Apr 22 '10 at 21:24






  • 1




    Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
    – OscarRyz
    Apr 22 '10 at 21:41


















up vote
2
down vote













You're trying to invoke an instance method on the class it self.



You should do:



    Random rand = new Random();
int a = 0 ;
while (!done) {
int a = rand.nextInt(10) ;
....


Instead



As I told you here stackoverflow.com/questions/2694470/whats-wrong...






share|improve this answer























  • does it still work if i have the first line in mane? (see my comments on Anthony's answer).
    – David
    Apr 22 '10 at 21:23










  • In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
    – OscarRyz
    Apr 22 '10 at 21:39










  • I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
    – Ruchir Baronia
    Dec 10 '15 at 5:53










  • @RuchirBaronia open a question and post your code, it should be easy to fix
    – OscarRyz
    Dec 10 '15 at 19:20


















up vote
1
down vote













In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.



Here's something you might want to do:



public class Foo
{
public void fee()
{
//do stuff
}

public static void main (Stringarg)
{
Foo foo = new Foo();
foo.fee();
}
}


Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.






share|improve this answer




























    up vote
    0
    down vote













    Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.



    The variable Random is only "in scope" inside the main method. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).



    If you want all of the methods of your class to use the same Random instance, declare a member variable:



    class MyObj {
    private final Random random = new Random();
    public void compTurn() {
    while (true) {
    int a = random.nextInt(10);
    if (possibles[a] == 1)
    break;
    }
    }
    }





    share|improve this answer




























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      27
      down vote



      accepted










      You are calling nextInt statically by using Random.nextInt.



      Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).



      It would be definitely worth while to check out:




      • What is the reason behind "non staticmethod cannot be referenced from a static context"?


      Update:



      You really should replace this line,



      Random Random = new Random(); 


      with something like this,



      Random r = new Random();


      If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.






      share|improve this answer























      • then i get a cannot find symbol error in regard to the r.
        – David
        Apr 22 '10 at 21:19










      • Did you initialize r? It can't just appear out of nowhere.
        – Anthony Forloney
        Apr 22 '10 at 21:20










      • yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
        – David
        Apr 22 '10 at 21:22










      • Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
        – Anthony Forloney
        Apr 22 '10 at 21:24






      • 1




        Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
        – OscarRyz
        Apr 22 '10 at 21:41















      up vote
      27
      down vote



      accepted










      You are calling nextInt statically by using Random.nextInt.



      Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).



      It would be definitely worth while to check out:




      • What is the reason behind "non staticmethod cannot be referenced from a static context"?


      Update:



      You really should replace this line,



      Random Random = new Random(); 


      with something like this,



      Random r = new Random();


      If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.






      share|improve this answer























      • then i get a cannot find symbol error in regard to the r.
        – David
        Apr 22 '10 at 21:19










      • Did you initialize r? It can't just appear out of nowhere.
        – Anthony Forloney
        Apr 22 '10 at 21:20










      • yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
        – David
        Apr 22 '10 at 21:22










      • Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
        – Anthony Forloney
        Apr 22 '10 at 21:24






      • 1




        Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
        – OscarRyz
        Apr 22 '10 at 21:41













      up vote
      27
      down vote



      accepted







      up vote
      27
      down vote



      accepted






      You are calling nextInt statically by using Random.nextInt.



      Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).



      It would be definitely worth while to check out:




      • What is the reason behind "non staticmethod cannot be referenced from a static context"?


      Update:



      You really should replace this line,



      Random Random = new Random(); 


      with something like this,



      Random r = new Random();


      If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.






      share|improve this answer














      You are calling nextInt statically by using Random.nextInt.



      Instead, create a variable, Random r = new Random(); and then call r.nextInt(10).



      It would be definitely worth while to check out:




      • What is the reason behind "non staticmethod cannot be referenced from a static context"?


      Update:



      You really should replace this line,



      Random Random = new Random(); 


      with something like this,



      Random r = new Random();


      If you use variable names as class names you'll run into a boat load of problems. Also as a Java convention, use lowercase names for variables. That might help avoid some confusion.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 23 '17 at 11:46









      Community

      11




      11










      answered Apr 22 '10 at 21:17









      Anthony Forloney

      69.8k12103111




      69.8k12103111












      • then i get a cannot find symbol error in regard to the r.
        – David
        Apr 22 '10 at 21:19










      • Did you initialize r? It can't just appear out of nowhere.
        – Anthony Forloney
        Apr 22 '10 at 21:20










      • yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
        – David
        Apr 22 '10 at 21:22










      • Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
        – Anthony Forloney
        Apr 22 '10 at 21:24






      • 1




        Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
        – OscarRyz
        Apr 22 '10 at 21:41


















      • then i get a cannot find symbol error in regard to the r.
        – David
        Apr 22 '10 at 21:19










      • Did you initialize r? It can't just appear out of nowhere.
        – Anthony Forloney
        Apr 22 '10 at 21:20










      • yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
        – David
        Apr 22 '10 at 21:22










      • Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
        – Anthony Forloney
        Apr 22 '10 at 21:24






      • 1




        Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
        – OscarRyz
        Apr 22 '10 at 21:41
















      then i get a cannot find symbol error in regard to the r.
      – David
      Apr 22 '10 at 21:19




      then i get a cannot find symbol error in regard to the r.
      – David
      Apr 22 '10 at 21:19












      Did you initialize r? It can't just appear out of nowhere.
      – Anthony Forloney
      Apr 22 '10 at 21:20




      Did you initialize r? It can't just appear out of nowhere.
      – Anthony Forloney
      Apr 22 '10 at 21:20












      yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
      – David
      Apr 22 '10 at 21:22




      yes i replaced both the second "Random" in main (so that it reads Random r = new Random();) and the "Random" comming before "nextInt"
      – David
      Apr 22 '10 at 21:22












      Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
      – Anthony Forloney
      Apr 22 '10 at 21:24




      Remove the Random declaration from main unless you necessarily need it, and just initialize Random r = new Random(); inside CompTurn
      – Anthony Forloney
      Apr 22 '10 at 21:24




      1




      1




      Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
      – OscarRyz
      Apr 22 '10 at 21:41




      Yes, it is, it is an object, but its scope is local to main, so it is not visible in other methods.
      – OscarRyz
      Apr 22 '10 at 21:41












      up vote
      2
      down vote













      You're trying to invoke an instance method on the class it self.



      You should do:



          Random rand = new Random();
      int a = 0 ;
      while (!done) {
      int a = rand.nextInt(10) ;
      ....


      Instead



      As I told you here stackoverflow.com/questions/2694470/whats-wrong...






      share|improve this answer























      • does it still work if i have the first line in mane? (see my comments on Anthony's answer).
        – David
        Apr 22 '10 at 21:23










      • In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
        – OscarRyz
        Apr 22 '10 at 21:39










      • I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
        – Ruchir Baronia
        Dec 10 '15 at 5:53










      • @RuchirBaronia open a question and post your code, it should be easy to fix
        – OscarRyz
        Dec 10 '15 at 19:20















      up vote
      2
      down vote













      You're trying to invoke an instance method on the class it self.



      You should do:



          Random rand = new Random();
      int a = 0 ;
      while (!done) {
      int a = rand.nextInt(10) ;
      ....


      Instead



      As I told you here stackoverflow.com/questions/2694470/whats-wrong...






      share|improve this answer























      • does it still work if i have the first line in mane? (see my comments on Anthony's answer).
        – David
        Apr 22 '10 at 21:23










      • In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
        – OscarRyz
        Apr 22 '10 at 21:39










      • I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
        – Ruchir Baronia
        Dec 10 '15 at 5:53










      • @RuchirBaronia open a question and post your code, it should be easy to fix
        – OscarRyz
        Dec 10 '15 at 19:20













      up vote
      2
      down vote










      up vote
      2
      down vote









      You're trying to invoke an instance method on the class it self.



      You should do:



          Random rand = new Random();
      int a = 0 ;
      while (!done) {
      int a = rand.nextInt(10) ;
      ....


      Instead



      As I told you here stackoverflow.com/questions/2694470/whats-wrong...






      share|improve this answer














      You're trying to invoke an instance method on the class it self.



      You should do:



          Random rand = new Random();
      int a = 0 ;
      while (!done) {
      int a = rand.nextInt(10) ;
      ....


      Instead



      As I told you here stackoverflow.com/questions/2694470/whats-wrong...







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited May 23 '17 at 10:31









      Community

      11




      11










      answered Apr 22 '10 at 21:21









      OscarRyz

      141k95336511




      141k95336511












      • does it still work if i have the first line in mane? (see my comments on Anthony's answer).
        – David
        Apr 22 '10 at 21:23










      • In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
        – OscarRyz
        Apr 22 '10 at 21:39










      • I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
        – Ruchir Baronia
        Dec 10 '15 at 5:53










      • @RuchirBaronia open a question and post your code, it should be easy to fix
        – OscarRyz
        Dec 10 '15 at 19:20


















      • does it still work if i have the first line in mane? (see my comments on Anthony's answer).
        – David
        Apr 22 '10 at 21:23










      • In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
        – OscarRyz
        Apr 22 '10 at 21:39










      • I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
        – Ruchir Baronia
        Dec 10 '15 at 5:53










      • @RuchirBaronia open a question and post your code, it should be easy to fix
        – OscarRyz
        Dec 10 '15 at 19:20
















      does it still work if i have the first line in mane? (see my comments on Anthony's answer).
      – David
      Apr 22 '10 at 21:23




      does it still work if i have the first line in mane? (see my comments on Anthony's answer).
      – David
      Apr 22 '10 at 21:23












      In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
      – OscarRyz
      Apr 22 '10 at 21:39




      In mane? Did you mean, in main ?? No, the random declared in main is local to that method. Once you get in a second method it is not reachable.
      – OscarRyz
      Apr 22 '10 at 21:39












      I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
      – Ruchir Baronia
      Dec 10 '15 at 5:53




      I get a null pointer exception when I do this though (by calling the method with an object)...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!)
      – Ruchir Baronia
      Dec 10 '15 at 5:53












      @RuchirBaronia open a question and post your code, it should be easy to fix
      – OscarRyz
      Dec 10 '15 at 19:20




      @RuchirBaronia open a question and post your code, it should be easy to fix
      – OscarRyz
      Dec 10 '15 at 19:20










      up vote
      1
      down vote













      In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.



      Here's something you might want to do:



      public class Foo
      {
      public void fee()
      {
      //do stuff
      }

      public static void main (Stringarg)
      {
      Foo foo = new Foo();
      foo.fee();
      }
      }


      Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.






      share|improve this answer

























        up vote
        1
        down vote













        In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.



        Here's something you might want to do:



        public class Foo
        {
        public void fee()
        {
        //do stuff
        }

        public static void main (Stringarg)
        {
        Foo foo = new Foo();
        foo.fee();
        }
        }


        Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.



          Here's something you might want to do:



          public class Foo
          {
          public void fee()
          {
          //do stuff
          }

          public static void main (Stringarg)
          {
          Foo foo = new Foo();
          foo.fee();
          }
          }


          Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.






          share|improve this answer












          In Java, static methods belong to the class rather than the instance. This means that you cannot call other instance methods from static methods unless they are called in an instance that you have initialized in that method.



          Here's something you might want to do:



          public class Foo
          {
          public void fee()
          {
          //do stuff
          }

          public static void main (Stringarg)
          {
          Foo foo = new Foo();
          foo.fee();
          }
          }


          Notice that you are running an instance method from an instance that you've instantiated. You can't just call call a class instance method directly from a static method because there is no instance related to that static method.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 22 '10 at 21:21









          artgon

          554159




          554159






















              up vote
              0
              down vote













              Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.



              The variable Random is only "in scope" inside the main method. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).



              If you want all of the methods of your class to use the same Random instance, declare a member variable:



              class MyObj {
              private final Random random = new Random();
              public void compTurn() {
              while (true) {
              int a = random.nextInt(10);
              if (possibles[a] == 1)
              break;
              }
              }
              }





              share|improve this answer

























                up vote
                0
                down vote













                Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.



                The variable Random is only "in scope" inside the main method. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).



                If you want all of the methods of your class to use the same Random instance, declare a member variable:



                class MyObj {
                private final Random random = new Random();
                public void compTurn() {
                while (true) {
                int a = random.nextInt(10);
                if (possibles[a] == 1)
                break;
                }
                }
                }





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.



                  The variable Random is only "in scope" inside the main method. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).



                  If you want all of the methods of your class to use the same Random instance, declare a member variable:



                  class MyObj {
                  private final Random random = new Random();
                  public void compTurn() {
                  while (true) {
                  int a = random.nextInt(10);
                  if (possibles[a] == 1)
                  break;
                  }
                  }
                  }





                  share|improve this answer












                  Violating the Java naming conventions (variable names and method names start with lowercase, class names start with uppercase) is contributing to your confusion.



                  The variable Random is only "in scope" inside the main method. It's not accessible to any methods called by main. When you return from main, the variable disappears (it's part of the stack frame).



                  If you want all of the methods of your class to use the same Random instance, declare a member variable:



                  class MyObj {
                  private final Random random = new Random();
                  public void compTurn() {
                  while (true) {
                  int a = random.nextInt(10);
                  if (possibles[a] == 1)
                  break;
                  }
                  }
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 22 '10 at 21:23









                  erickson

                  219k42327422




                  219k42327422















                      Popular posts from this blog

                      Trompette piccolo

                      Slow SSRS Report in dynamic grouping and multiple parameters

                      Simon Yates (cyclisme)