Page 1 of 1

About Question enthuware.ocpjp.v7.2.1243 :

Posted: Tue Jul 02, 2013 8:54 pm
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 ?

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

Posted: Wed Jul 03, 2013 5:51 am
by admin
Yes, that is correct.

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

Posted: Tue Aug 26, 2014 1:05 am
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..?

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

Posted: Tue Aug 26, 2014 4:24 am
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.

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

Posted: Sat Sep 26, 2015 5:18 am
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.

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

Posted: Sat Sep 26, 2015 7:05 am
by admin
No, r is static, not the anonymous class.

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

Posted: Sun Sep 27, 2015 7:07 am
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?

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

Posted: Sun Sep 27, 2015 9:14 am
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.

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

Posted: Sun Oct 11, 2015 4:57 pm
by danillosl
Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
what that supposed to mean?

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

Posted: Sun Oct 11, 2015 9:42 pm
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.

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

Posted: Tue Jan 09, 2018 7:49 pm
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");
        }
    }
}

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

Posted: Tue Jan 09, 2018 9:38 pm
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.

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

Posted: Thu Jan 11, 2018 11:32 pm
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.

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

Posted: Fri Jan 12, 2018 12:07 am
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.