Page 1 of 1

About Question enthuware.ocpjp.v8.2.1383 :

Posted: Mon Dec 07, 2015 8:30 am
by Tariee
Its me again :D

I understand anonymous classes can never be static even when created inside a static method but how about when referenced by a static variable?

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Mon Dec 07, 2015 9:22 am
by admin
In that case the reference variable is static, not the class.

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Sun Apr 15, 2018 6:57 am
by shamran99
Hi

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?

Regards,
Shamran.

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Sun Apr 15, 2018 12:59 pm
by admin
No, they do have a name. It is just that the name is generated by the compiler. They are actually final. You can prove that using javap.

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Thu May 10, 2018 10:14 am
by AnthRuos
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.

What am I missing?

Thanks!

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Thu May 10, 2018 11:58 am
by admin
The option is talking about the main method given in the question.

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Fri Nov 05, 2021 12:49 pm
by Javatje
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?

Re: About Question enthuware.ocpjp.v8.2.1383 :

Posted: Sat Nov 06, 2021 5:22 am
by admin
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.

Example:

Code: Select all

public class TestClass1{
  public static void main(String args[])
   {
        new TestClass().new A();  //valid
	TestClass tc = new TestClass();
        tc.new A(); //valid
   }
}