About Question enthuware.ocpjp.v8.2.1383 :
Moderator: admin
-
- Posts: 5
- Joined: Wed Sep 02, 2015 7:21 pm
- Contact:
About Question enthuware.ocpjp.v8.2.1383 :
Its me again
I understand anonymous classes can never be static even when created inside a static method but how about when referenced by a static variable?
I understand anonymous classes can never be static even when created inside a static method but how about when referenced by a static variable?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
In that case the reference variable is static, not the class.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Wed May 10, 2017 2:49 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
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.
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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Jun 21, 2016 9:37 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
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!
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!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
The option is talking about the main method given in the question.
If you like our products and services, please help us by posting your review here.
-
- Posts: 9
- Joined: Wed Jun 30, 2021 6:53 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
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?
And how can an instance of the class a outside of TestClass ever be initialized?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1383 :
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:
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
}
}
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: rwilliams93 and 104 guests