Difference between namespace in C# and package in Java

前端 未结 6 541
日久生厌
日久生厌 2020-11-28 20:32

What is the difference (in terms of use) between namespaces in C# and packages in Java?

6条回答
  •  时光说笑
    2020-11-28 21:09

    There's no such term as "namespace" in Java - a package acts as a namespace in Java though, in terms of providing a scope for names. It's also part of the accessibility model.

    From section 7 of the Java Language Specification:

    Programs are organized as sets of packages. Each package has its own set of names for types, which helps to prevent name conflicts. A top level type is accessible (§6.6) outside the package that declares it only if the type is declared public.

    EDIT: Okay, after the clarification: a Java package is similar to a C# namespace - except that it has an impact on accessibility, whereas in C# namespaces and accessibility are entirely orthogonal.

提交回复
热议问题