Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Greenhorn
Posts: 13
3
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 10782
230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the terms "imports" and "has access to" are used interchangeably.
 
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Jj Roberts
Greenhorn
Posts: 13
3
MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 14373
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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: 66236
250
  • Mark post as helpful
  • send pies
  • 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.
 
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!