Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Sat Feb 02, 2013 8:28 pm
by ETS User
interface XMen {
void shoot(String a);
}

public class WarZone {
public static void main(String[] args){
XMen x = null;
if(args.length() > 0){
x = new XMen(){
...

Confused why I got this wrong because you cannot instantiate an instance of an interface, am I wrong? I even tried this myself and got a compile error like I expected.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Sat Feb 02, 2013 10:25 pm
by admin
Did you try the exact same code as given in the question?

You are right that an interface cannot be instantiated. But we are not instantiating an interface here. We are instantiating an anonymous class that implements the interface.

If you are interested to learn more, try this: after compiling the program, check how many .class files are created. One of the files will have a $ in its name. Run javap on that class. That will illustrate what is happening in this question.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Sat Jun 15, 2013 4:45 pm
by el_bar
ok .. what is result if its args.length

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Aug 28, 2013 6:25 am
by DesRenthuware
... just my ten cents. Also got this wrong on the test, but then I had not prepared by studying inner classes (not in exam objectives - so prep only done using pertinent sections of K&B6 and the Mala Gupta book). My real question then is - is Oracle likely to do something like this on the real test?

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Aug 28, 2013 8:35 am
by admin
No, We recently updated the problem statement to include a note on the top:
"Note: This question may be considered too advanced for this exam."

Please make sure you are using the latest version of the question bank.

There are a few questions that touch upon these topics and we have included these just to make sure the reader has complete grasp of the subject. But if you are short on time, you may ignore these. (All of them have the same note at the top so that it is easy to identify.)

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Aug 28, 2013 9:01 am
by DesRenthuware
Thanks for the feedback.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Thu Oct 03, 2013 9:50 am
by TheSarjo
sorry, i don't know if i have a problem with my version of java (i'm on mac, and it's still java 6 running on it).

i tried to run javap on the class file named WarZone$1, but... nothing lights my mind. And there is no difference if i run it without $1...

here is what it gives me:

Code: Select all

javap WarZone$1
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
    public WarZone();
    public static void main(java.lang.String[]);
}



Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Thu Oct 03, 2013 12:42 pm
by admin
When you compile the given code, what all .class files do you see generated?
-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Mon Oct 07, 2013 8:46 am
by TheSarjo
When i compile WarZone.java i get 3 classes:
WarZone.class
WarZone$1.class
XMen.class

Here is compilation commands
iMac-di-Alex:ciao Sarjo$ javac WarZone.java
iMac-di-Alex:ciao Sarjo$ javap WarZone$1
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
public WarZone();
public static void main(java.lang.String[]);
}

iMac-di-Alex:ciao Sarjo$ javap WarZone
Compiled from "WarZone.java"
public class WarZone extends java.lang.Object{
public WarZone();
public static void main(java.lang.String[]);
}

iMac-di-Alex:ciao Sarjo$ javap XMen
Compiled from "WarZone.java"
interface XMen{
public abstract void shoot(java.lang.String);
}

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Mon Oct 07, 2013 1:12 pm
by admin
Not sure why your javap is not showing the details of the anonymous class. When I do javap on WarZone$1, I see this:
final class WarZone$1 extends java.lang.Object implements XMen{
WarZone$1();
public void shoot(java.lang.String);
}
I don't have access to mac.

-Paul.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Oct 09, 2013 4:11 am
by TheSarjo
thank you, now it's more clear

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Mar 12, 2014 9:18 pm
by fasty23
if we use args.length() compiler assumed 'self" is an string and if we use args.length compiler assumed "self" is an array?
sorry for this lame question, somebody explain this question in this way but i'm not sure it is correct.

Re: About Question enthuware.ocajp.i.v7.2.1122 :

Posted: Wed Mar 12, 2014 9:24 pm
by admin
Compiler doesn't assume anything. i.e whether args is a String or array. Compiler knows what args is. Based on what args is, it will either allow args.length() or allow args.length.
Let me ask it to you in another way. How do you call a method on an object and how do you access a variable of an object?