About Question enthuware.ocpjp.v7.2.1233 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1233 :

Post 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.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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.

scopert
Posts: 1
Joined: Tue Apr 15, 2014 12:32 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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 ?

andresv
Posts: 1
Joined: Thu Oct 09, 2014 3:36 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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()."

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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)

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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!

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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().

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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();
    }
}

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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.

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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.

krohani
Posts: 31
Joined: Tue Oct 06, 2015 1:57 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post 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!

admin
Site Admin
Posts: 10386
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1233 :

Post by admin »

What happened when you tried it out?

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests