The article directories

  • 1. The difference between public class and class
  • 2. Conclusion:

1. The difference between public class and class

  1. You can define more than one Java source fileclass
  2. A Java source filepublictheclassIt’s not necessary
  3. aclassWill define and generate onexxx.classBytecode file
  4. Defining public classes in a Java source file,There can only be one, and the class name must be andjavaThe source files have the same name
  5. eachclassCan be writtenmainMethods can be set to the entry of the program to executionB.classIn themainMethods:java BAnd want to performx.classThe main method in:java x
  6. Pay attention to: When executed in a command windowjava Hello, then the requirementhello.classThere has to be a master method.
  • Public static void main (string[]args) public static void main (string[]args)

Example: Define an ABc.java

class A{}class B{}class C{}Copy the code
  • usejavacThe command is compiled to generate three bytecodes, respectivelyA.class, B.class, C.class. Perform:javac ABC.java, will produce three.classfile

  • Through the above tests can be concluded: onejavaMore than one can be defined in the source fileclass, and one at compile time

    classOne will be generated for compilationclassBytecode files. Also,publicclassNo.
  • Next, in theABC.javaThe source code continues to define a”Public class D“, please see the picture below

  • As you can see, the definition is publicClass DAfter compiling again, the compiler reported an error. And the error message is:

    Class D is public and should be in the nameD.javaDeclaration in the file. In other wordsA.javaThe public class name defined in the file must beA, cannot be any other name. Also indirectly stated in the samejavaThere can be only one public class in a file

    In the same placejavaClass names in files cannot be the same.
  • Through the above tests can be concluded: if the definitionpublic classClass, can define only one, and the class name is required

    To be andjavaThe source file names must be the same. (Just remember the rules).
  • Next, it is defined in the class body of each classmainMethod, write the entry of the program to see if it can compile and run:
class A{
	public static void main(String[] args){
		System.out.println("A..."); }}class B{
	public static void main(String[] args){
		System.out.println("B..."); }}class C{
	public static void main(String[] args){
		System.out.println("C..."); }}public class ABC{
	public static void main(String[] args){
		System.out.println("ABC..."); }}Copy the code
  • javac ABC.java

  • Enter an entry function from a random class:Java A, Java B

  • You can see that in every one of themclassCan be writtenmainMethod to load the class from which you want the program to execute

2. Conclusion:

  • Through the above tests can be concluded: anyclassYou can set the program entry to any of themclass

    Can be writtenmainMethod (main method), from which entry to execute, let the class loader load the corresponding class first

    Yes, for example: want to make class AmainMethod executes, then executes:java A, want to make class BmainMethod is executed, then

    Perform:java B. But actually, for a complete, standalone application,All you need to do is provide an entrance, which means

    All you need to do is define a main method.
  • Also, in actual development, although onejavaMultiple source files can be definedclassIn fact, it’s not normative,

    The standard way to write it is onejavaOnly one is defined in the source fileclass.