A detailed explanation of the difference between public class and class in Java

Source: Internet
Author: User
Tags definition class definition naming convention
The following is an analysis of the difference between public class and class in Java, the need for friends can come to reference the next

You can define a class in two ways when writing a class:
public class definition class:
Class Definition classes:
if a class declaration is declared using public class, the class name must be exactly the same as the file name.
Example: Defining a Class (filename called: Hello.java)

Copy Code code as follows:


public class hellodemo{//Declare a class, a naming convention for class names: capitalize the first letter of all words


public static void Main (String args[]) {//Main method


System.out.println ("HelloWorld!!!"); System output, print
on screen

    }


};


This class uses the public class declaration, the class name is Hellodemo, but the file name is Hello.java, so the following issues occur at compile time :

Copy Code code as follows:


Hello.java:1 class Hellodemo is public and should be declared in a Hellodemo.java file named


public class hellodemo{//Declare a class, the naming specification for class names: capitalize all words in the first letter


1. Error
The above error hint indicates that because the public class declaration is used, the class name should be exactly the same as the file name, that is, "Hellodemo.java" should be used to represent the name of the class.
If the declaration of a class uses class, the class name can be inconsistent with the file name, but execution is definitely performed with the generated name.
Example: The following code (the filename is called: Hello.java)

Copy Code code as follows:


class hellodemo{


public static void Main (String args[]) {


System.out.println ("HelloWorld!!!");


    }


};


The filename is called Hello.java, the file name is inconsistent with the class name, but because the class declaration is used, the compilation does not produce any errors at this time, but the name of the *.class file after the build is exactly the same as the class name declared: Hellodemo.class
Execution can no longer execute Java Hello, but should execute Javahellodemo

In a *.java file, there can be only one declaration of public class, but a declaration that allows more than one class

Copy Code code as follows:


public class hello{


public static void Main (String args[]) {


SYSTEM.OUT.PRINTLN ("HelloWorld!!!");


    }


};


class a{};


class b{};


In the above file, three classes are defined, and three *.class files are formed when the program is compiled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.