Why don't I have to import a class I just made to use it in my main class? (Java)

前端 未结 9 757
星月不相逢
星月不相逢 2021-02-05 02:30

I am currently learning Java using the Deitel\'s book Java How to Program 8th edition (early objects version).

I am on the chapter on creating classes and methods.

相关标签:
9条回答
  • 2021-02-05 03:08

    It is because both are in same package(folder). They are automatically imported no need to write import statement for that.

    0 讨论(0)
  • 2021-02-05 03:10

    The classes located in the same package do not have to be imported, as they are visible to each other. You simply import a class that is in another package:

    import java.util.ArrayList;
    

    Note that you are not importing the file, but the class.

    0 讨论(0)
  • 2021-02-05 03:15

    You don't have to import classes that are in the same package as the current class.

    Also, note that GradeBook.java is the name of the file. The (simple) name of the class is GradeBook. Every class should be in a package. If it is in the package com.foo.bar, the class name is com.foo.bar.GradeBook, and this is the name you must use when importing this class.

    Read http://download.oracle.com/javase/tutorial/java/package/packages.html to learn more about packages.

    0 讨论(0)
提交回复
热议问题