Java files

Question: Can a”.java” source file contain multiple classes (not inner classes)? What are the restrictions?

Answer: There can be more than one class, but only one public class, and the public class name must match the file name. A file can have only non-public classes. If there is only one non-public class, the class can be different from the filename.

Why can there be only one public class in a Java source file?

In the Book Java Programming Minds (4th edition) there are three paragraphs (6.4 class access) like this:

  • Each compilation unit (file) can have only one public class, which means that each compilation unit has a single public interface, represented by the public class. The interface can contain as many classes as required that support package access rights. If there is more than one public class in a compilation unit, the compiler gives an error message.
  • The name of the public class must be exactly the same as the filename containing the compilation unit, and case sensitive. If they do not match, you will also get a compilation error.
  • Although not very common, it is possible to compile units without public classes at all. In this case, you can name the file whatever you want.

Summarize several relevant issues:

  1. Can a “.java “source file contain multiple classes (not inner classes)? What are the restrictions?

A: There can be more than one class, but only one public class, and the public class name must match the file name.

  1. Why can only have one public class in a file

A: The compiler accepts only one public class for a Java source file (also known as a “compilation unit”) at compile time. Otherwise, an error is reported.

  1. Can there be no public class in a Java file

A: Public classes are not required; Java files can have no public classes.

  1. Why must the class name of the public class be the same as the file name

A: To facilitate the VM to find the bytecode file corresponding to the corresponding class in the corresponding path.

2. Main method

Main function: a special function that acts as an entry point to the program and can be called by the JVM

Main function definition:

Public: indicates that the function has the maximum access permission

Static: indicates that the main function exists when the class is loaded

Void: The main function has no concrete return value

Main: Not a keyword, but a special word that is recognized by the JVM

(String[] args) : the argument to the function. The argument type is an array of elements in the array. Main (String[] args) This space-time array of strings has a length of 0, but arguments can also be passed to it at run time.

The main function is fixed format, recognized by the JVM

Main functions can be overridden, but the JVM recognizes only main (String[] args), and the rest as generic functions. The arGS knowledge array variables can be changed, but nothing else can be changed.

A Java file can contain many classes, each with one and only one main function, but each Java file can contain many main functions, and at run time the JVM entry needs to be specified. For example, the main function of one class can call the main function of another class. The main function of the public class is not necessarily used.

Access permissions for external classes

External classes can only be decorated with public and default.

Why modify an external class or class?

  1. There is a package concept: public and default can distinguish between the external class and the different packages can be divided (default class, other packages can not import this class, public class can only be imported)
  2. Protected is visible within the package and its subclasses are visible, but when an external class wants to extend a protected class from a different package class, it won’t find that class, let alone several layers
  3. A private modified external class that cannot be imported by any other external class.
Public class {} public class {// The access permission for non-public open classes is package by default. // An external class has only two types of access, one is visible inside the package, and the other is visible outside the package. // If you use the private modifier, other classes cannot see the class at all, which makes no sense. // If you use protected, it is still visible within the package, but if a subclass wants to extend the class, but different packages, // can not find the class, and cannot inherit it, so use default instead. class A { }Copy the code

Java package naming rules

  • The core Java packages that begin with Java.* are the classes that all programs use.
  • “Javax.*” is an extension package. X stands for extension. Although Javax.* is an improvement and extension of Java.*, due to the increasing use of Javax.*, many programs rely on Javax. It’s part of the core and shipped with the JDK.
  • The ones starting with org.* are packages released by organizations or organizations that, because of their influence and high quality code, also ship some of their commonly used classes with the JDK.

In terms of package naming, there is a convention to prevent duplication of names: Everyone with their own domain name inverted form as the beginning of their development of the package named, such as Baidu released the package will begin with com.baidu.*, W3C released the package will org.w3c.*, micro college released the package will net. Weixueyuan.

An organization’s domain name suffix is org, and a company’s domain name suffix is com. Packages starting with org.* can be considered as packages released by non-profit organizations. They are generally open source and can be used for free in their products without concern for infringement, while packages starting with com. There may be copyright issues, so use with care.

5. Use of import

If you want to use a class from a Java package, you must first import the class with the import statement syntax:

The import package1 [. Package2...]. .classname; Package is the package name, classname is the classname. For example, import java.util.Date; // Import java.util.Scanner from Date class java.util; // Import the Scanner class import javax.swing.* from the java.util package. // Import all classes in the javax.swing package, where * denotes all classesCopy the code

Note:

Import can only import classes contained in a package, not a package. For convenience, instead of importing individual classes, we import all classes in the package, such as import java.util.*; .

The Java compiler imports all classes in the JDK java.lang package for all Java programs by default (import java.lang.*;). , which defines common classes such as System, String, Object, Math, etc., so we can use these classes directly without explicitly importing them. But other classes must be imported first.

The “Hello World” program uses system.out.println (); Statement, the System class is in the java.lang package. Although we did not explicitly import the classes from this package, the Java compiler has already imported them for us by default, otherwise the program will fail.

Search path for Java classes Java programs import corresponding classes when they run, which is the process of loading.class files. Suppose we have the following import statement:

import p1.Test;
Copy the code

This statement indicates that the Test class in the P1 package is to be imported.

When installing the JDK, we have set the environment variable CLASSPATH to indicate the path to the library, which has a value of. %JAVAHOME%\lib, D:\Program Files\jdk1.7.071, CLASSPATH =. D: \ \ Program Files \ jdk1.7.071 \ lib.

If the desired class file is found at the first path, the search stops, otherwise the search continues at later paths, and if the desired class file is not found at all paths, the compile or run error occurs.

You can add a search path to the CLASSPATH variable, for example. %JAVA_HOME%\lib; C:\javalib, then you can put the class files in the C:\javalib directory, the Java runtime will find the same.

The IDE can specify the output directory of the compiled class file for user-written classes. Appclassloader will load the classes to the specified directory

Here is an example of a class that imports both access rights:

Classes in package com.javase.java. A package; Public class global access to classes in {} package com.javase.java. A package; Class package access {} package com.javase.java classes; //import can import base packages as well as public classes. Use the full path of the class name // and when importing a package.*, the subpackage classes are not imported to avoid import errors. // Note that //import the classes in com.javase.java. A package. // This import will report an error because the class is not public and cannot be imported with import. Import classes in com.javase.java. A package. Global access; // It can be imported.Copy the code

Six, summarized

I believe you already know what you usually ignore Java basic knowledge. Point attention, don’t get lost, follow programmer Zeng Zeng, share different Java basics every day.