• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Does a compilation unit import the package it is a member of?

 
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I am using the Enthuware 1z0-815 practice test to prepare for the OCP  Java 11 exam (1z0-815). I have some questions regarding question #14 on the first test.

The question asks which packages are automatically imported by a Java file which has no package statement. The answer given is that java.lang and the unnamed package are automatically imported. The explanation is

If there is no package statement in the source file, the class is assumed to be created in an unnamed package that has no name. In this case, all the types created in this package will be available to this class without any import statement.


I am a little confused by this. While this is true, to my mind the fact that all of the types are available does not necessarily mean that the unnamed package is imported. The most relevant info I have found so far is from the JLS (Java Language Specification), at the beginning of Chapter 7.

Code in a compilation unit automatically has access to all types declared in its package and also automatically imports all of the public types declared in the predefined package java.lang .


Code in named packages has access to all types declared in its package too. I thought that since the source file is part of the unnamed package, it automatically has access to all of the types declared in the unnamed package, and therefore would not have to import it. I also performed a search for the unnamed package in the JLS 11 document, and I couldn’t find anything about it being imported.

My question: does a compilation unit with no package statement import the unnamed package? Also, more generally: does a compilation unit import its own package implicitly?
 
Saloon Keeper
Posts: 14488
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the terms "imports" and "has access to" are used interchangeably.
 
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:

... In this case, all the types created in this package will be available to this class without any import statement.


I am a little confused by this. While this is true, to my mind the fact that all of the types are available does not necessarily mean that the unnamed package is imported.


Nowhere in the quoted statement does it say that classes in the unnamed package are imported. Seems you're reading too much into that statement.
 
Jesse Duncan
Bartender
Posts: 387
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That quote is the explanation of the answer; it is the answer which I was wondering about. The question asks which packages are imported, and the answer is java.lang and the package with no name (unnamed package).
 
Junilu Lacar
Sheriff
Posts: 17264
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you're saying now. On one hand, I doubt there's a different mechanism the compiler uses to automatically put java.lang classes and those in the unnamed package in scope so if java.lang is "automatically imported" then by the same token, it's not a stretch to reason that classes in the unnamed packages are also treated the same way. On the other hand, if you're really going to be a stickler for semantics and the JLS doesn't say that classes in the unnamed package are automatically imported but still somehow are accessible, then you have an argument that the statement "unnamed packages are automatically imported" is subject to debate.

I wouldn't worry about it too much because I don't think the exam is going to be that nit-picky, if that's your concern. All that matters is that you understand that java.lang.* classes and those in the unnamed package will be accessible to a class that doesn't have a package declaration.
 
Marshal
Posts: 76802
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know how pedantic I can be; that tells me that no classes in the current package (named or unnamed) are imported because they don't need to be imported, and there is an implicit import of everything public in the java.base/java.lang package. Even if you don't write import java.lang.String;
But I think Junilu is right: the exam will be written so there is no ambiguity so I don't think this semantic pedantry will make any difference to your exam.
 
No prison can hold Chairface Chippendale. And on a totally different topic ... my stuff:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic