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

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

Moderator: admin

Post Reply
Javanaut

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

Post by Javanaut »

So in this question an import is needed even to access a public class from a different package? I thought a public class could be accessed from any package since its public... 0_o

dtchky
Posts: 19
Joined: Wed Aug 01, 2012 3:11 am
Contact:

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

Post by dtchky »

Yes, an import is needed based on the answer options available : )

In a way you kind of answered your own question. You can't make use of a separate package within your application unless you properly import the parts of it you require, or specify the full hierarchy. A public method can be instantiated/accessed without an import only if it forms part of the same package. If the public method is located in a different package then you need to import it or include the hierarchy.

Eg:

Code: Select all

class MyClass {
    public static void main(String[] args) {
        java.util.ArrayList<String> al = new java.util.ArrayList<String>();
    }
}
Or

Code: Select all

import java.util.ArrayList;
class MyClass {
    public static void main(String[] args) {
        ArrayList<String> al = new ArrayList<String>();
    }
}

Michailangelo

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

Post by Michailangelo »

I thought the statement "import static com.foo.X.*;" imports only the static members of X not all the members of X.

satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

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

Post by satar »

Exactly what Michailangelo said. I was stumped on this one because I still don't know how the main method in class Y can create an instance of class X if all it has is:

import static com.foo.X.*;

In this case, I would think all it has access to is the static members, which would only be LOGICID. How, can it create an instance of X without:

import com.foo.*;

I see the question doesn't specifically say independent of each other, but it also does not say both would be required and in fact, if you have: import com.foo.*; you wouldn't need import static com.foo.X.*; Honestly, I don't think this question is consistent and just causes a costly amount of time trying to read into it, unless I am missing something.

With this said, I will point out that I have the utmost respect for this exam suite. I wish it didn't ask so many questions that I understand will not be on the exam like memorization of range constraints on primitive types or detailed knowledge about wrapper classes, as initially it cost some good practice time as I stumbled on them while taking the mock exams. However, it is obviously good to know them. Anyway, I digress but wanted to say that overall this is an excellent prepping/learning system.

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

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

Post by admin »

Hi,
Actually, the question is quite simple. Here is why:
1. The question says:
//1 <== INSERT STATEMENT(s) HERE
So it should be clear that you *may* have to write more than one statement here.

2. It is also given that you have to select two options.

3. As mentioned in the above discussion, import static imports only the static elements of a class. So that will not work for X x = new X(). This keyword static in import static is kind of a dead giveaway here.

So you need import com.foo.X or com.foo.* as well.

------------------------------------
Regarding extraneous questions, I agree there are some questions that you may feel are not required but they are there either to make you get the complete picture of a particular topic or to make sure you are surprised in the exam because they are on borderline. It is for your own good. So please work with them and you will be happy later :)

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

jmrego
Posts: 8
Joined: Wed Dec 10, 2014 6:05 am
Contact:

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

Post by jmrego »

Can´t understand why the last option is invalid: import com.foo.X.LOGICID -> "Syntax for importing static fields is: import static <package>.<classname>.*; or import static <package>.<classname>.<fieldname>;

Syntax looks correct: package "com.foo", class "X", fieldname "LOGICID".

OK. static is missing. :roll: My bad.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM »

Hi,

I am not sure if I understood right here, so please Admin can you verify if what am I saying here is right? Thanks

In case of using this line:
import imports.com.foo.*;
everything from package foo is imported, including class X, that is why we can have X x = new X()
But this does not allow me to access the public static members of X. (correct? also, I am a bit surprised because they are public, shouldn't I be able to access all public members of a class?)

If I want to use the static members of X (by using only their name), I should statically import its members:
import static imports.com.foo.X.*;

Is this true also for classes available in the same package? Thanks

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Hi,

Kind of the same question as above. Is it the case that

Code: Select all

import com.foo.*; 
doesn't cover the use of LOGICID in class Y, because LOGICID is a static field?

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

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

Post by admin »

Here is what you need to understand:
1. To access anything (a class or a member of a class), first, it needs to be accessible to others. That is, it should have appropriate access modifier. For example, a class with default access cannot be accessed from another package, no matter what import statement we use. We are not talking about that case here.

2. Next, when you use a class name in your code, the compiler has to figure out which class you mean. Obviously, you can have a class named X in package x and a class named X also in package y. How will the compiler know which class you mean if you write X x; in your code? In needs to have some way of disambiguating this name.

3. One way to help the compiler is to always use the complete name of class to access that class from other classes. i.e. com.foo.X will always refer to the class X in package com.foo. Compiler knows which class you mean when you use the complete name (also known as Fully Qualified Class Name.) Similarly, you can use the complete name of a static variable to access a static variable of another class. Therefore, com.foo.X.LOGICID correctly refers to the static field LOGICID of class X.

3. Now, since writing the complete name every time is too cumbersome, Java provides a shortcut. You can use an import statement to import all (again, all meaning the classes that are accessible) classes of a package. For example, import com.foo.*; means that whenever the compiler finds a class name in your code that it is not able to figure out, it will go and check if that class exists in com.foo package. If it does, it will assume that you indeed meant com.foo.X when you wrote X.

4. So far, we only have a way to disambiguate the class name. To access a static member of a class, you still have to do ClassName.fieldName. For example, X.LOGICID or com.foo.X.LOGICID.

5. Java provides another shortcut to reduce the typing even further by letting you import the static fields of a class. For example, import static com.foo.X.*; tells the compiler to check the class X if it is not able to figure out which field is implied by LOGICID.


Now, to answer your question, "Is it the case that import com.foo.*; doesn't cover the use of LOGICID in class Y, because LOGICID is a static field?"
That is exactly right. If you want to refer to LOGICID of Y, you still need to use Y.LOGICID.
If you like our products and services, please help us by posting your review here.

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Woah! I will consider that carefully. Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 239 guests