0
package ttt2;
import java.util.Scanner;
public class TTT2 {
    public class State{
        int[][] sheet;
        int childCount;
        public void initialize(int n,int[][] lastState,int level){
            sheet=new int[n][n];
            childCount=n*n-1;
            State[] nextState=new State[childCount];
            nextState[0].initialize(n,sheet,level+1);
            int turn=level%2+1;
        }
    }
    public static void main(String[] args) {
        System.out.print("Enter the size(n) of the sheet(n*n):");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[][] matrix=new int[n][n];
        State s=new State();
    }

}

I am having this issue no matter what I try. I tried everything i could. While declaring an object of the State class it is showing an error "non-static variable this cannot be referenced from a static context"

5 Answers5

2

It's because non-static inner classes require an instance of an enclosing class to be instantiated, and you are trying to do this from a static context:

public class TTT2 {
    public class State{ // <- non-static
    }
    public static void main(String[] args) {
        State s=new State(); // <- static context
    }
}

You have two options. One is to make the inner class static as well, so it no longer requires an enclosing class instance:

public class TTT2 {
    public static class State{ // <- static
    }
    public static void main(String[] args) {
        State s=new State(); // <- no problem
    }
}

The other is to instantiate a new TTT2 to use as an enclosing instance, the downside is you've created a TTT2 that you probably won't use for anything else (at least in your example):

public class TTT2 {
    public class State{ // <- non-static
    }
    public static void main(String[] args) {
        State s=new TTT2().new State(); // <- no problem
    }
}
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • I have searched for its answer already but didn't get ... that's why asked. – Laxmikant Revdikar Aug 13 '14 at 09:35
  • 1
    @LaxmikantRevdikar No worries. The duplicate status wasn't a criticism. Your particular problem can be hard to search for because it's clouded by unrelated causes. Marking as a duplicate is meant to help you (you can now see some more answers), other readers (who can now find the original), and the answerer of the original (who deserves credit for his answer). It also helps keep noise down on the site. – Jason C Aug 13 '14 at 09:37
1

Though I did not get specified exception rather it shows(in eclipse)

No enclosing instance of type TTT2 is accessible. Must qualify the allocation with an enclosing instance of type TTT2 (e.g. x.new A() where x is an instance of TTT2).

And

State s=new TTT2().new State(); 

This is the correct way to create innerclass object creation.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

There is no enclosing instance of the State class. Either create an outer instance of TTT2 or declare State as static

State state = new TTT2().new State();
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Declare you State class as

public static class State {
    // ...
}

And you should be set. This is because the State class, without the static modifier, can only be accessed through an instance of the holding class, therefore, a static method should not be able to access it because it wouldn't know which instance to use.

Making State static allows it to e instantiated without the instance of the outer class.

1

As State is a nested class of TTT2, you get a State object for every TTT2 object - so you should either declare State as public static class State when using in a static context or instantiating it using new TT2().new State();

Smutje
  • 17,733
  • 4
  • 24
  • 41