About Question enthuware.ocpjp.v7.2.1243 :

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

Moderator: admin

Post Reply
renatumb
Posts: 47
Joined: Mon Apr 08, 2013 7:55 pm
Contact:

About Question enthuware.ocpjp.v7.2.1243 :

Post by renatumb »

I could understand what you mean with:
Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
Would it be a constructor that take some parameters ?

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

mue.con
Posts: 3
Joined: Mon Jul 29, 2013 7:27 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by mue.con »

Anonymous inner classes cannot have any 'extends' or 'implements' clause
Is'nt it, that it cannot have any clause. But you can extend or implement classes and interfaces by for example:

Code: Select all

MyInterface mi = new MyInterface() {
 public void myInterfaceMethod(){}
};
So the second answer should be wrong..?

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

Yes, an anonymous class can extend another class or implement an interface. It cannot have an the extends or implements clause in its declaration.
Second option is indeed wrong.
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by pbonito »

Anonymous inner classes cannot be static.

class Example{

static Runnable r = new Runnable(){public void run(){System.out.println("I'm static");}};

public static void main(String ... args){
Thread t = new Thread(r);
t.start();

}

}
Above class compile and run.

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

No, r is static, not the anonymous class.
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by pbonito »

Ok, If we see in this way "Anonymous inner classes cannot be static." is a nonsense.
I can't figure out what kind of modifier an anomymous class may have.
None?

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

Well, Java Language specification section 15.9.5 clearly says:
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
The same section also tells exactly what kind of modifiers an anonymous class has -
An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.
An anonymous class is never abstract (§8.1.1.1).
An anonymous class is always implicitly final (§8.1.1.2).
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

danillosl
Posts: 2
Joined: Sat Aug 29, 2015 9:00 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by danillosl »

Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
what that supposed to mean?

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

danillosl wrote:
Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
what that supposed to mean?
Please read the explanation. It shows exactly what the above statement means. It contains code that creates an anonymous class with an initialization argument.
If you like our products and services, please help us by posting your review here.

zukras
Posts: 5
Joined: Fri Jun 02, 2017 4:57 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by zukras »

Tried and it is correct only for class fields. Methods are class members as well but they cannot be declared as static. So, is it correct statement saying that inner class can have static members?
A non static inner class may have static members.
Example:

Code: Select all

public class Outer {
    class Inner {
        static final int i = 0; // fine

        static final void print() { // compile error
            System.out.println("Inner static method");
        }
    }
}

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

Yes, that is why this option is marked correct. A non-static inner class may have static fields if you make them final.
If you like our products and services, please help us by posting your review here.

zukras
Posts: 5
Joined: Fri Jun 02, 2017 4:57 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by zukras »

But correct answer is "A non static inner class may have static members". Since method is a member of class as well why then the is not "A non static inner class may have static fields."? Details but I think terminology should be used right.

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

Re: About Question enthuware.ocpjp.v7.2.1243 :

Post by admin »

I understand what you are saying but the statement is legally correct. It does not say that all kinds of members can be static. It just says it may have static members. field is a member and it can be static.
Yes, "fields" would be more clear but you may expect some ambiguity in the real exam as well.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests