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

前端 未结 9 756
星月不相逢
星月不相逢 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 02:52

    It's all about packages. You are trying to use a class from the default package which does not need explicit import of the java file, ie GradeBook inside GradeBookTest

    Here is where you can start with learning about packages :

    Java Package Tutorial

    and :

    Creating and Using Packages

    0 讨论(0)
  • 2021-02-05 02:54

    You don't have to import classes which are in the same package.

    0 讨论(0)
  • 2021-02-05 02:56

    Well, classes in the same package are automatically imported.

    0 讨论(0)
  • 2021-02-05 02:57

    Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them.

    From: Include one java file in another java file

    0 讨论(0)
  • 2021-02-05 02:59

    They're in the same package. This tutorial will do more justice than I will.

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

    Imports are for importing classes that are in a different package. Since you didn't declare a package for either they are both put in the default package. The compiler can find it because the class lives in the same directory.

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