Kamis, 05 September 2024

Java Packages Notes 1

  • Every class is part of some package.
  • All classes in a file are part of the same package.
  • You can specify/create the package using a package declaration:
    package name;
    as the first (non-comment) line in the java source code file.
  • Multiple files can specify the same package name.
  • If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files).
  • Package names are written in all lowercase. Companies often use their reversed internet domain name to start their package names.
  • If package name is specified, the file must be in a subdirectory/folder called same as the folder name (i.e., the directory name must match the package name).
  • You can access public classes in another (named) package without to include the package-name (at the beginning of the file) using full qualified name like this:
    package-name.class-name
  • You can access the public fields and methods without to include the package-name (at the beginning of the file), using of such classes using full qualified name like this:
    package-name.class-name.field-or-method-name
  • You can include the package-name using:
    import package-name.*;
    or
    import package-name.class-name;
    at the beginning of the file (after the package declaration). The former imports all of the classes in the package, and the second imports just the named class. You must still use:
    class-name to access the classes in the packages, and
    class-name.field-or-method-name to access the fields and methods of the class; the only thing you can leave off is the package name.

Bibliography

https://www.dnpgcollegemeerut.ac.in/ ( university )
https://www.wisc.edu/ ( university )
https://docs.oracle.com/

Tidak ada komentar:

Posting Komentar

Various Other Posts