Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

First some code:

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

class TicTacToe 
{
//...

public static void main (String[]arg) 
{ 

    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?

like image 741
David Avatar asked Apr 22 '10 21:04

David


People also ask

Can you access a non-static variable in the static context?

Of course, they can, but the opposite is not true, i.e. you cannot obtain a non-static member from a static context, i.e. static method. The only way to access a non-static variable from a static method is by creating an object of the class the variable belongs to.

Why can't we call non-static method from static method?

A non-static method is dependent on the object. It is recognized by the program once the object is created. But a static method can be called before the object creation. Hence you cannot make the reference.

What does Cannot be referenced from a static context mean?

Posted on 7th November 2022. Hey Kusum, The non-static variable cannot be referenced from a static context is compiler error that occurs when the user tries to put program code to access a non-static variable inside main in Java that is static.

Why we use non-static method in Java?

A non-static method in Java can access static methods and variables as follows: A non-static method can access any static method without creating an instance of the class. A non-static method can access any static variable without creating an instance of the class because the static variable belongs to the class.


2 Answers

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.

like image 154
Anthony Forloney Avatar answered Sep 17 '22 17:09

Anthony Forloney


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...

like image 30
OscarRyz Avatar answered Sep 19 '22 17:09

OscarRyz