You have said that "Anonymous classes are implicitly final."
The only way to prove this, I can say that the anonymous inner classes can not be extended. Its because that class hasn't got an associated name with it. Am I correct?
Hi,
you wrote:"class created inside the main method is final"
Because it is anonymous. Anonymous classes are implicitly final.
but if I write
public class TestE {
public static void main(String[] args) {
class CreatedInTheMain{
}
class CCC extends CreatedInTheMain{
}
}
}
I don't get any compile error
and CreatedInTheMain is not an anonymous class.
How come there is no conflict between the two classes that are both named a?
And how can an instance of the class a outside of TestClass ever be initialized?
1. They are in different scopes. One is a top level class and the other one is inside the TestClass. It is an inner class.
2. You can create an instance of the inner class A in any other class like this:
new TestClass().new A();
Since A is an instance member of TestClass, you need an instance of TestClass for an instance of A to exist.
public class TestClass1{
public static void main(String args[])
{
new TestClass().new A(); //valid
TestClass tc = new TestClass();
tc.new A(); //valid
}
}