Page 1 of 1

How do Imports work?

Posted: Fri Jun 02, 2023 8:22 pm
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

Re: How do Imports work?

Posted: Fri Jun 02, 2023 11:03 pm
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.