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

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

Moderator: admin

Post Reply
Ant

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

Post by Ant »

Hi,
I think there's a mistake in the answer of this question.

Here question:
Which of the given options should be inserted at line 1 so that the following code can compile without any errors?

Code: Select all

package objective1; 
// 1 
public class StaticImports{  
        public StaticImports(){  
   out.println(MAX_VALUE);   
  }     
 }  
(Assume that java.lang.Integer has a static field named MAX_VALUE)

Here answer:
You had select two option
import static java.lang.Integer.*;
static import java.lang.System.out;
static import Integer.MAX_VALUE;
import static java.lang.System.*;
static import java.lang.System.*;
Only one of anwer variant is rigth, not two, here is
import static java.lang.Integer.*;

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

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

Post by admin »

Hi,
Why do you think so?
Paul.
If you like our products and services, please help us by posting your review here.

Ant

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

Post by Ant »

Because, "static import ..." - is wrong import statement.
And "java.lang.System." has't value "MAX_VALUE".
Only "java.lang.Integer" has static value "MAX_VALUE"

Please correct me if I'm wrong.

Ant

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

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

Post by admin »

Ant wrote:Because, "static import ..." - is wrong import statement.
And "java.lang.System." has't value "MAX_VALUE".
Only "java.lang.Integer" has static value "MAX_VALUE"

Please correct me if I'm wrong.

Ant
MAX_VALUE is in java.lang.Integer, but "out" is in java.lang.System.
So, the correct options are :
import static java.lang.Integer.*;
import static java.lang.System.*;

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Ant

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

Post by Ant »

Thanks, now everything is clear.

Ant.

smearaDubha
Posts: 6
Joined: Wed Apr 17, 2013 6:35 am
Contact:

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

Post by smearaDubha »

Hi

Instead of

import static java.lang.System.*;

Should it not have been :

import java.lang.System.*;

seen as you are using methods in in the System class?

Cheers
Richie

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

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

Post by admin »

No, because System class is in java.lang package, which is imported by default.
But the static fields of any class have to be imported explicitly (if you want them to be imported.)

-Paul.
If you like our products and services, please help us by posting your review here.

smearaDubha
Posts: 6
Joined: Wed Apr 17, 2013 6:35 am
Contact:

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

Post by smearaDubha »

Hi Paul

Thanks for the reply, I'm still a little confused though. There are 2 static imports required, but I can only see 1 static variable being used?

Thanks
Richie

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

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

Post by admin »

There are two static variables being used - out and MAX_VALUE.
If you like our products and services, please help us by posting your review here.

smearaDubha
Posts: 6
Joined: Wed Apr 17, 2013 6:35 am
Contact:

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

Post by smearaDubha »

OK thanks, I wasn't aware that out was a variable

Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

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

Post by Wisevolk »

Hello,

I think there another good answer :

static import java.lang.System.out;

With this the code will compile also without any error.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

You must specify the full package name of the class that you are importing (just like the regular import statement)
Should this be member rather than class?

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

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

Post by admin »

You are taking the statement out of context. It is explaining that you can't do
import static Integer.*
You need to give the full package name of the class Integer here i.e. import static java.lang.Integer.* to import all the static members of the class Integer.
If you like our products and services, please help us by posting your review here.

Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

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

Post by Daniel Clinton »

Ah yes, sorry
jumped the gun there :|

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests