When you define multiple classes in a source file, and you also have import statements and package statements, pay particular attention to these rules:
- There can be only one public class in a source file.
- A source file can have multiple non-public classes.
- The name of the source file should be consistent with the class name of the public class. For example: The class name of the public class in the source file is employee, then the source file should be named Employee.java.
- If a class is defined in a package, then the packages statement should be in the first row of the source file.
- If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be first in the source file.
- The import statement and the package statement are valid for all classes defined in the source file. In the same source file, you cannot give different package declarations to different classes.
- The class has several access levels, and the classes are divided into different types: abstract and final classes, and so on. These will be described in subsequent chapters.
- In addition to the several types mentioned above, Java also has some special classes, such as inner classes, anonymous classes.
A simple example
In this example, we create two classes of Employee and Employeetest, respectively, in the package P1 and P2.
The employee class has four member variables, namely name, age, designation, and salary. The class explicitly declares a constructor method that has only one parameter.
In Eclipse, create a package named P1, create a class in the package, name Employee, and copy the following code into the source file:
1 PackageP1;2 Public classemployee{3 String name;4 intAge ;5 String designation;6 Doublesalary;7 //How to construct the Employee class8 PublicEmployee (String name) {9 This. Name =name;Ten } One //set the value of age A Public voidEmpage (intempage) { -Age =Empage; - } the //set the value of the designation - Public voidempdesignation (String empdesig) { -designation =Empdesig; - } + //set the value of the salary - Public voidEmpsalary (Doubleempsalary) { +Salary =empsalary; A } at //Output Information - Public voidPrintemployee () { -System.out.println ("Name:" +name); -System.out.println ("Age:" +Age ); -SYSTEM.OUT.PRINTLN ("designation:" +designation); -System.out.println ("Salary:" +salary); in } -}
The program starts with the main method. In order to run this program, you must include the main method and create an object.
The following is a employeetest class that creates two employee objects and invokes a method to set the value of a variable.
In Eclipse, create a package that is named P2, create a class in the package, name Employeetest, and copy the following code into the source file:
1 PackageP2;2 Importp1.*;3 Public classemployeetest{4 Public Static voidMain (String args[]) {5 //Create two objects6Employee Empone =NewEmployee ("James Smith");7Employee Emptwo =NewEmployee ("Mary Anne");8 //call the member methods of both objects9Empone.empage (26);TenEmpone.empdesignation ("Senior software Engineer"); OneEmpone.empsalary (1000); A Empone.printemployee (); -Emptwo.empage (21); -Emptwo.empdesignation ("Software Engineer"); theEmptwo.empsalary (500); - Emptwo.printemployee (); - } -}
Compile and run the Employeetest class, and you can see the following output:
Name:james Smith
Age:26
Designation:senior Software Engineer
salary:1000.0
Name:mary Anne
Age:21
Designation:software Engineer
salary:500.0
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (26) claim rules for source files