About Question enthuware.ocpjp.v7.2.1233 :
Moderator: admin
-
- Posts: 17
- Joined: Wed Jan 22, 2014 12:35 pm
- Contact:
About Question enthuware.ocpjp.v7.2.1233 :
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.
It seems like statements new B() or new B() {} seem to work.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
In general, when you nest a class inside another class, you can mark it static and have static fields inside it. For example:
However, when you create an anonymous class, you can't have static fields in it because an anonymous inner class cannot be static.
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.
-
- Posts: 1
- Joined: Tue Apr 15, 2014 12:32 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
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 ?
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 ?
-
- Posts: 1
- Joined: Thu Oct 09, 2014 3:36 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
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()."
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
Also don't understand about new TestClass.A().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 ?
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();
}
}
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)
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
Why do you expect new TestInner().A(); to work? This is a syntax for calling a method named A() on new TestInner object!
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
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().
Could I ask to give some code explaining how "new TestClass.A()" working on instantiating of inner A().
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
Answer doesn't say new TestClass().A() will work.
It says new TestClass.A() will work.
Here is your code that compiles fine:
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();
}
}
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
Hi,
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.
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.
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.
Why do you think that the code compiles?Here is your code that compiles fine
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.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
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:
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.
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 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.
-
- Posts: 31
- Joined: Tue Oct 06, 2015 1:57 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
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!
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!
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1233 :
What happened when you tried it out?
Who is online
Users browsing this forum: No registered users and 3 guests