Page 1 of 1

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

Posted: Wed Oct 17, 2012 2:50 pm
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.*;

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

Posted: Wed Oct 17, 2012 4:04 pm
by admin
Hi,
Why do you think so?
Paul.

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

Posted: Wed Oct 17, 2012 4:37 pm
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

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

Posted: Wed Oct 17, 2012 5:01 pm
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.

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

Posted: Thu Oct 18, 2012 4:32 am
by Ant
Thanks, now everything is clear.

Ant.

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

Posted: Mon Apr 22, 2013 3:23 pm
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

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

Posted: Mon Apr 22, 2013 4:42 pm
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.

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

Posted: Mon Apr 22, 2013 5:01 pm
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

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

Posted: Mon Apr 22, 2013 5:28 pm
by admin
There are two static variables being used - out and MAX_VALUE.

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

Posted: Tue Apr 23, 2013 6:15 am
by smearaDubha
OK thanks, I wasn't aware that out was a variable

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

Posted: Thu Jul 18, 2013 6:11 pm
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.

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

Posted: Wed Oct 15, 2014 5:11 am
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?

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

Posted: Wed Oct 15, 2014 11:03 am
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.

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

Posted: Wed Oct 15, 2014 12:22 pm
by Daniel Clinton
Ah yes, sorry
jumped the gun there :|