Page 1 of 1
About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Jan 22, 2014 2:43 pm
by EpicWestern
In the explanation, what does "an anonymous class can never be static" mean?
It seems like statements new B() or new B() {} seem to work.
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Jan 22, 2014 10:11 pm
by admin
In general, when you nest a class inside another class, you can mark it static and have static fields inside it. For example:
Code: Select all
class A{
static class B{
static int x = 10;
}
}
However, when you create an anonymous class, you can't have static fields in it because an anonymous inner class cannot be static.
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Tue Apr 15, 2014 12:53 pm
by scopert
new TestClass().new A();
new TestClass.B();
new A();
new TestClass.A();
I don't understand how new TestClass.A() is working. A is not static, shouldn't it be linked to an instance of class TestClass ?
However it doesn't work in a static method. Can someone explain ?
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Oct 29, 2014 3:21 pm
by andresv
It think you didn't read the explanation section carefully enough: "Now, the method useclasses() is an instance method. So, it already has instance of outer class associated with it. So, new A(); is also valid. new TestClass.A(); is same as new A()."
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Sun Aug 09, 2015 2:47 am
by alkour
scopert wrote:new TestClass().new A();
new TestClass.B();
new A();
new TestClass.A();
I don't understand how new TestClass.A() is working. A is not static, shouldn't it be linked to an instance of class TestClass ?
However it doesn't work in a static method. Can someone explain ?
Also don't understand about new TestClass.A().
Class A - is inner class, not static nested class like class B.
so it could be instantiated only:
new A() // within outer class;
new TestClass. new A() // outside and inside outer class
new TestClass.A() - it should correct, when class A() static nested class.
To test I ran the code:
Code: Select all
public class TestInner {
public class A{}
public static class B {}
public void useClasses() {
new TestInner().new A();
new A();
new TestInner.B();
new TestInner().A();
}
public static void main(String[] args) {
TestInner t = new TestInner();
t.useClasses();
}
}
It will not compile, because of "new TestInner().A();" with error:
Code: Select all
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: testinner.TestInner.A
at testinner.TestInner.useClasses(TestInner.java:11)
at testinner.TestInner.main(TestInner.java:15)
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Sun Aug 09, 2015 10:22 am
by admin
Why do you expect new TestInner().A(); to work? This is a syntax for calling a method named A() on new TestInner object!
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Sun Aug 09, 2015 3:10 pm
by alkour
I did not expect new TestInner().A(); working. I wanted to prove that it does not work unlike the answer.
Could I ask to give some code explaining how "new TestClass.A()" working on instantiating of inner A().
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Sun Aug 09, 2015 9:02 pm
by admin
Answer doesn't say new TestClass().A() will work.
It says new TestClass.A() will work.
Here is your code that compiles fine:
Code: Select all
public class TestInner {
public class A{}
public static class B {}
public void useClasses() {
new TestInner().new A();
new A();
new TestInner.B();
new TestInner.A();
}
public static void main(String[] args) {
TestInner t = new TestInner();
t.useClasses();
}
}
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Aug 19, 2015 3:43 am
by alkour
Hi,
Answer doesn't say new TestClass().A() will work.
It says new TestClass.A() will work.
Sorry. Do not understand this phrase. First line does not saying that new TestClass().A() will work. Second line saying that new TestClass().A() is working. They are opposite meaning.
Here is your code that compiles fine
Why do you think that the code compiles?
Have you tried? I gave the error thrown while it compiles.
Why I am asking to clarify how
new TestClass.A() (where A() non-static, inner class) works, because it contradicts with JLS.
Or may be I do not understand something.
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Aug 19, 2015 6:30 am
by admin
1. I am really sorry but I have not idea what you are saying. I checked each option and they look fine to me. Could you please post a screenshot of what you see and which option do you think has a problem?
2. Yes, the code that I gave you compiles fine. I did compile and check before posting.
3. Regarding new TestClass.A(), you need to understand that TestClass.A is just a syntax. Java designers have allowed it. Nothing much you can do about it. It has nothing to do with static or instance. It compiles because you are doing new TestClass.A() inside an instance method of TestClass. Therefore, the method already executes within the context of a TestClass instance (remember "this"?) You cannot do new TestClass.A() from a static method.
You can add a constructor to class A and inside the constructor, print TestInner.this like this:
Code: Select all
public class A{
public A(){
System.out.println("In A cons: "+TestInner.this);
}
}
You will see that the values printed by new TestClass.A() and new TestClass().A() are different. That means, they use a different instance of TestClass but there is an instance of TestClass associated with A.
You cannot A a = new TestClass.A() in a static method (there is no "this" in static context). You have to do A a = new TestInner().new A();
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Jan 27, 2016 1:09 pm
by krohani
So as the explanation says, we are allowed to use new TestClass.A() becuase it is being called from an instance method of class TestClass. This leads me to 2 related questions:
1. If we were in an instance method of another class say TestClass2, could we still simply call new TestClass.A()? My guess would be no.
2. If we were in a static method of class TestClass then instead of calling new TestClass.B() could we simply do new B()?
Thanks!
Re: About Question enthuware.ocpjp.v7.2.1233 :
Posted: Wed Jan 27, 2016 1:56 pm
by admin
What happened when you tried it out?