Question : nested classes limitations

Hi,

I read somewhere that limitations of nested classes are

a. nested interfaces are always implicitly static
b. inner classes may not declare static members(with exception of compile -time constant fields)

I did not understood what ihese limitations means.Can you please advise me.  Any links, resources, suggestions, sample code highly appreciated. Thanks in advance

Answer : nested classes limitations

(a.)  Just means that you don't have to put static in front of an inner interface.  It can always be referenced, while an inner class will produce compile-time errors if it's used outside of the classes unless it's static.  See first code snippet attached.

(b.) This seems to be false, as I can do the following without any errors:

class MyClass1 {
      public static class MyInnerClass1 {
            public static int a = 0;

            public static void method() {
            }
      }
}

public class WhiteMage {
      public static void main(String[] args) {
            MyClass1.MyInnerClass1.a = 45;
            MyClass1.MyInnerClass1.method();
      }
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
class MyClass1 {
	public static interface MyInnerInterface1 {
	}

	public interface MyInnerInterface2 {
	}

	public static class MyInnerClass1 {
	}

	public class MyInnerClass2 {
	}
}

//no errors
class MyClass2 implements MyClass1.MyInnerInterface1,MyClass1.MyInnerInterface2 {
}

/**
 *
 * @author WhiteMage at http://www.experts-exchange.com/
 */
public class WhiteMage {
	public static void main(String[] args) {
		MyClass1.MyInnerInterface1 a = new MyClass2(); //no error
		MyClass1.MyInnerInterface2 b = new MyClass2(); //no error
		MyClass1.MyInnerClass1     c = new MyClass1.MyInnerClass1(); //no error

		//ERROR!
		MyClass1.MyInnerClass2     d = new MyClass1.MyInnerClass2();
	}
}
Random Solutions  
 
programming4us programming4us