Page 1 of 1

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

Posted: Fri Nov 08, 2013 11:12 am
by matheuscs
Here is the question:
Given the following contents of two java source files:

Code: Select all

package util.log4j; 
public class Logger  {   
    public void log(String msg){       
        System.out.println(msg);   
    } 
}
and

Code: Select all

package util; 
public class TestClass {
    public static void main(String[] args) throws Exception {
        Logger logger = new Logger();
        logger.log("hello");
    }
}
What changes, when made independently, will enable the code to compile and run?
The answer
Replace package util.log4j; with
package util;
makes me believe that it will only and simply rename the package, which would cause the Logger class unreachable.

Wouldn't it be good if the answer was changed to say also that the file should be moved from location?

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

Posted: Fri Nov 08, 2013 11:37 am
by admin
Hi,
Why would the Logger class be unreachable? The Java file can reside at any location irrespective of the pacakge. The class files will automatically go in the right directory if compiled with -d option. Since the question is not asking about the details of compilation, you can assume that right compilation switches are being used.

HTH,
Paul.

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

Posted: Sun Jun 22, 2014 9:17 am
by sarakh
But if we
Replace package util.log4j; with
package util;
then we will have a package called util which has two public methods:
1. public class Logger
2. public class Logger
And I guess there can be only 1 public class in each package. right?

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

Posted: Sun Jun 22, 2014 8:20 pm
by admin
sarakh wrote:But if we
Replace package util.log4j; with
package util;
then we will have a package called util which has two public methods:
1. public class Logger
2. public class Logger
And I guess there can be only 1 public class in each package. right?
1. I not sure which two methods are you talking about. You've quoted the same class twice.
2. No, you can have as many public classes in a package as you want. You cannot have more than one public class in one file because a public class must always be in a file with the same name.

HTH,
Paul.

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

Posted: Tue Jun 24, 2014 11:44 am
by sarakh
Hi Paul,

I meant we will have "public class TestClass" and "public class Logger".
But yes you are right. I was confused with files and packages. Thank you for the explanation.

Sara

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

Posted: Wed Nov 25, 2015 4:40 pm
by NickWoodward
Answer 3:
In this case, you are importing package util
Are we? Where?

Nick

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

Posted: Wed Nov 25, 2015 6:54 pm
by admin
All the classes of the same package are already imported in a class. But you are right. importing is a poor choice of word here. Fixed.

thanks for your feedback!

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

Posted: Wed Nov 25, 2015 7:02 pm
by NickWoodward
ah, ok. yeah, i did find it a little confusing!

thanks for the quick reply!