About Question enthuware.ocajp.i.v7.2.1122 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1122 :
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.
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Jun 15, 2013 4:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
ok .. what is result if its args.length
-
- Posts: 32
- Joined: Wed Aug 28, 2013 6:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
... 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?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
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.
"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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 32
- Joined: Wed Aug 28, 2013 6:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
Thanks for the feedback.
-
- Posts: 15
- Joined: Fri Jul 12, 2013 12:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
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:
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[]);
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
When you compile the given code, what all .class files do you see generated?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri Jul 12, 2013 12:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
When i compile WarZone.java i get 3 classes:
WarZone.class
WarZone$1.class
XMen.class
Here is compilation commands
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);
}
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
Not sure why your javap is not showing the details of the anonymous class. When I do javap on WarZone$1, I see this:
-Paul.
I don't have access to mac.final class WarZone$1 extends java.lang.Object implements XMen{
WarZone$1();
public void shoot(java.lang.String);
}
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri Jul 12, 2013 12:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
thank you, now it's more clear
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
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.
sorry for this lame question, somebody explain this question in this way but i'm not sure it is correct.
Last edited by fasty23 on Wed Mar 12, 2014 9:30 pm, edited 1 time in total.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1122 :
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?
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?
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 161 guests