How do Imports work?

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
aks0961
Posts: 12
Joined: Sun May 28, 2023 1:17 pm
Contact:

How do Imports work?

Post by aks0961 »

Hi, I am on chapter 4 learning about packages and imports right now and I just had a few questions.

So, for example, on page 60 where it states "Important points about the import statement" and under point #4, it states something along the lines of "java.lang is imported automatically into ALL THE CLASSES". The statement "all the classes" is just throwing me off a bit

I understand that imports basically just take the classes from different packages (or even the same packages) and basically make it known to the compiler that we may or may not be using aspects of other packages/classes in this program

But when the book says "all the classes" it makes it seem like every class in the src will have an import, however, I thought it would be more like how packages are made (which is declaring the package file once at the top of the program)

So I just wanted some clarification on how imports work and when are where they are declared, if you don't mind

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

Re: How do Imports work?

Post by admin »

Not sure what you mean by " (or even the same packages)". An import statement is not needed to refer to classes in the same package.

Did you read section 4.3.3 from the beginning? and the "Note" above "Important points..."? You might want to go through this section again carefully because all your doubts are actually covered there.

I am not sure what you mean by "declaring the package file ". Where are you getting this from? There is nothing like a "package file" in Java.
Yes, every java file has an implicit import statement for the java.lang package. That is why don't need to write it explicitly.

BTW, If you are jumping across sections in the book, I would suggest you to read the book sequentially otherwise you will be spending a lot of time in solving doubts.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests