I understand anonymous classes can never be static even when created inside a static method but how about when referenced by a static variable?
About Question enthuware.ocpjp.v8.2.1383 :
Moderator: admin
-
Tariee
- 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?
-
admin
- Site Admin
- Posts: 10437
- 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.
-
shamran99
- 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.
-
admin
- Site Admin
- Posts: 10437
- 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.
-
AnthRuos
- 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!
-
admin
- Site Admin
- Posts: 10437
- 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.
-
Javatje
- 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?
-
admin
- Site Admin
- Posts: 10437
- 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
}
}
Who is online
Users browsing this forum: No registered users and 104 guests