Would it be a constructor that take some parameters ?Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
About Question enthuware.ocpjp.v7.2.1243 :
Moderator: admin
-
- Posts: 47
- Joined: Mon Apr 08, 2013 7:55 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1243 :
I could understand what you mean with:
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Mon Jul 29, 2013 7:27 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
Is'nt it, that it cannot have any clause. But you can extend or implement classes and interfaces by for example:Anonymous inner classes cannot have any 'extends' or 'implements' clause
Code: Select all
MyInterface mi = new MyInterface() {
public void myInterfaceMethod(){}
};
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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.
Second option is indeed wrong.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
No, r is static, not the anonymous class.
If you like our products and services, please help us by posting your review here.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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?
I can't figure out what kind of modifier an anomymous class may have.
None?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
Well, Java Language specification section 15.9.5 clearly says:
Paul.
The same section also tells exactly what kind of modifiers an anonymous class has -An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).
HTH,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).
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Sat Aug 29, 2015 9:00 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
what that supposed to mean?Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
Please read the explanation. It shows exactly what the above statement means. It contains code that creates an anonymous class with an initialization argument.danillosl wrote:what that supposed to mean?Anonymous inner classes can never have initialization parameters.
They can if they are for classes.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Jun 02, 2017 4:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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?
Example:A non static inner class may have static members.
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");
}
}
}
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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.
-
- Posts: 5
- Joined: Fri Jun 02, 2017 4:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1243 :
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.
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.
Who is online
Users browsing this forum: No registered users and 116 guests