Case sensitivity of Java class names

前端 未结 3 948
天涯浪人
天涯浪人 2020-12-04 16:45

If one writes two public Java classes with the same case-insensitive name in different directories then both classes are not usable at runtime. (I tested this on Windows, Ma

3条回答
  •  攒了一身酷
    2020-12-04 17:20

    Don't think just about folders.

    Use explicit different namespaces ("packages") for your classes, and maybe use folders to match your classes.

    When I mention "packages", I don't mean "*.JAR" files, but, just the concept of:

    package com.mycompany.mytool;
    
    // "com.mycompany.mytool.MyClass"
    
    public class MyClass
    {
       // ...
    } // class MyClass
    

    When you do not specify a package for your code, the java tools (compiler, I.D.E., whatever), assume to use the same global package for all. And, in case of several similar classes, they have a list of folders, where to look for.

    Packages are like "virtual" folders in your code, and apply to all your packages on your classpath, or installation of Java. You can have several classes, with the same I.D., but, if they are in different package, and you specify which package to look for, you won't have any problem.

    Just my 2 cents, for your cup of Java coffe

提交回复
热议问题