I am testing the snippet below, I need to know how can I access t.x or t.hello? What is its scope? Do developers define variables in this way?
public class Test{ public Test(){ System.out.print("constructor\n"); } public static void main(String[] args) { Test t = new Test(){ int x = 0; //System.out.print("" + x); void hello(){ System.out.print("inside hello\n"); } }; } Edit
But why this snippet worked
Thread tr = new Thread() { int loops = 1; @Override public void run() { loops += 1; } }; tr.start();
start()is a method ofThread. The variabletris also of typeThread, so you can call its methods. Just not new methods you add in an AIC.