About Question enthuware.ocpjp.v8.2.1383 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Tariee
Posts: 5
Joined: Wed Sep 02, 2015 7:21 pm
Contact:

About Question enthuware.ocpjp.v8.2.1383 :

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

In that case the reference variable is static, not the class.

shamran99
Posts: 15
Joined: Wed May 10, 2017 2:49 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

AnthRuos
Posts: 1
Joined: Tue Jun 21, 2016 9:37 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

The option is talking about the main method given in the question.

Javatje
Posts: 9
Joined: Wed Jun 30, 2021 6:53 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
   }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests