Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sun Nov 04, 2012 10:39 am
by ETS User
Hi guys.
I am working with this question and it seems that there is a mistake.
Which of the given options if put at //1 will correctly instantiate objects of various classes defined in the following code?
Code: Select all
public class TestClass
{
public class A
{
}
public static class B
{
}
public void useclasses()
{
//1
}
}
one of the proper anwser is
new TestClass.A();
My JVM says it is wrong. Can you tell me what is wrong?
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sun Nov 04, 2012 11:31 am
by admin
Are you sure you ran the exact same code as given in the question? I just tried it and it works fine.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Fri Oct 11, 2013 11:26 am
by Lodengruen
The problem is the missing semicolon in the proposed answer from the exam, option 4 is:
new TestClass.A()
but the proper answer would be:
new TestClass.A();
as you noted above!
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Fri Oct 11, 2013 11:28 am
by Lodengruen
btw: What can I do if I find such a mistake in a question during the exam?
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sat Oct 12, 2013 3:53 am
by admin
Hi,
I do see ; in that option. Please make sure you are using the latest version of the question bank.
If you get such error in the exam, you may ignore the typo if it is obvious and pick it as the right option. But there is no guarantee as to what the question setter had in mind while writing the question so your guess is as good as mine.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sun Dec 13, 2020 3:09 am
by borkutip
Hello,
Code: Select all
class E1 {
public int a;
public class A {};
public void mI() {
A a1 = new A();
A a2 = this.new A();
A a3 = new E1.A();
A a4 = new E1().new A();
// E1.a = 5;
}
}
I see and know, that all the above instantiations work, but I do not understand, how
A a3 = new E1.A() could work.
class A is not static, how can I reference to it with E1.A ?
I thought, that similarly to
E1.a (which obviously does not work)
new E1.A() also should not work.
Moreover, It also does not clear for me, how
new E1(). new A() or
this.new A() could work. As I understand, dot operator is stronger than new operator so (based on my mental model, which must be bad, but I do not know where is its fallacy)
new this.A() should work, but in reality, this is not working.
Thank you in advance
Péter
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sun Dec 13, 2020 3:51 am
by admin
>how A a3 = new E1.A() could work.
Because this statement is already inside an instance method of E1, so, there is already an instance of E1 associated when you do new E1.A(). Try it from a static method of E1 and see what happens.
Where did you read that the dot operator is stronger than new?
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Mon Dec 14, 2020 3:00 pm
by borkutip
Thank you for your answer.
(dot and new precedence): I read it here:
https://introcs.cs.princeton.edu/java/11precedence/
This is not very authentic, but at Oracle, I found only much less information:
https://docs.oracle.com/javase/tutorial ... ators.html
Here, there are many missing, for example, dot, new, cast, etc.
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Mon Dec 14, 2020 9:10 pm
by admin
and is also incorrect because if dot had higher precedence than new, the following line wouldn't compile:
System.out.println(new String("hello").length()); //prints 5
This shows that new has higher (or at least same) precedence than dot.
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Sat Dec 19, 2020 7:53 am
by borkutip
Thank you for your answer, your example is great.
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Tue Oct 04, 2022 2:51 pm
by edufin166@yahoo.com
Sorry but, I can not understand WHY new TestClass.A(); Is being used to call a Instance-Class..
For Public Static Class B, thats ok... so new TestClass.B() BEcause I do not need a instance od TestClass, in order to, use Class B once it is static.
But for public class A . I believe that the correte would be: new TestClass().A();
Am i wronged??
Re: About Question com.enthuware.ets.scjp.v6.2.703 :
Posted: Tue Oct 04, 2022 11:47 pm
by admin
Yes, since A is not a static class, an instance of its outer class is needed. But if you put the statement new TestClass.A(); within an instance method of the outer class (which is what the question is asking), then it already has a reference to an instance of the outer class at that point (the "this" reference). So, new TestClass() is not required there.