Home>

Java source file declaration rules

When multiple classes are defined in a source file,And when there are import statements and package statements, pay special 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 the same as 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 package statement should be on the first line 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 come first in the source file.

The import and package statements are valid for all classes defined in the source file.In the same source file,You cannot declare different packages for different classes.

Classes have several access levels,And classes are also divided into different types:abstract classes and final classes. These will be introduced 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 employee and employeetest and put them in packages p1 and p2 respectively.

The employee class has four member variables,They are name, age, designation, and salary. This class explicitly declares a constructor method,This method has only one parameter.

In eclipse, create a package,Name it p1, create a class in this package,Name it employee and copy the following code into the source file:

package p1;
public class employee {
 string name;
 int age;
 string designation;
 double salary;
 //constructor of employee class
 public employee (string name) {
  this.name=name;
 }
 //Set the value of age
 public void empage (int empage) {
  age=empage;
 }
 //Set the value of designation
 public void empdesignation (string empdesig) {
  designation=empdesig;
 }
 //Set the value of salary
 public void empsalary (double empsalary) {
  salary=empsalary;
 }
 //output information
 public void printemployee () {
  system.out.println ("name:" + name);
  system.out.println ("age:" + age);
  system.out.println ("designation:" + designation);
  system.out.println ("salary:" + salary);
 }
}

Programs are executed from the main method.In order to run this program,You must include the main method and create an object.

The employeetest class is given below. This class creates two employee objects and calls methods to set the value of the variable.

Create another package in eclipse,Name it p2, create a class in this package,Name it employeetest and copy the following code into the source file:

package p2;
import p1. *;
public class employeetest {
 public static void main (string args []) {
  //create two objects
  employee empone=new employee ("james smith");
  employee emptwo=new employee ("mary anne");
  //call member methods of these two objects
  empone.empage (26);
  empone.empdesignation ("senior software engineer");
  empone.empsalary (1000);
  empone.printemployee ();
  emptwo.empage (21);
  emptwo.empdesignation ("software engineer");
  emptwo.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

Emphasize programming style

Although the code style does not affect the operation of the program,But the readability of the program is very important.Make your own programs understandable to others,First, pay great attention to typography.

In fact, everyone's programming style and the programming style of each software development company are different.Program code written by one person,It should be understandable by others,Even after a long time,I have to understand it myself.Otherwise this program becomes a dead program.

Programming style refers to the format when programming,Make the program look layered.Here are some examples.Explain the importance of programming style:

public class math {
 public static void main (string [] args) {
  int x=12;
  double y=12.3d;
  void print () {
   char a="a";
   system.out.println (a);
  }
  system.out.println (x + y);
 }
}

Does the entire layout of the above program look comfortable?And a strong sense of hierarchy?Do you know the entire program architecture at a glance?The key here is indentation,Indentation can also be called tabbing.

The above code uses the indentation:"public class math" is top grid,Then the mian () method is indented by 4 spaces, the code inside the mian () method is always indented by 8 spaces, and the body code of the print () method is indented by 4 spaces. In this way, the ownership of the entire program is obvious.The mian () method belongs to the math class, the rest belong to the main () method, and the code snippet inside the print () method belongs to this method.The rule is that code with more spaces is subordinate to code with fewer spaces.

I recommend that you use the tab key for indentation. Most editors (such as eclipse) support the custom number of spaces for the tab key.Usually 4 spaces.

In addition to indentation,A blank line is also necessary,Look at the following program code:

public class math {
 public static void main (string [] args) {
  int x=12;
  int y=23;
  void print () {
   //.......
  }
  void view () {
   //..........
  }
 }
}

The above program segment,There is a blank line between the print () method and the view () method,Used to distinguish between different modules.The print () method is not the same as the view () method,So use blank lines to separate them,This makes the program more readable.

Also, note the method or property names.These names should have meaning,Better to be regular,Do n’t just use common variables like "a", "b",You can name variables or functions as appropriate.The "print" above, other programmers know this method at a glance,Is a function related to printing or output.Another example:the variable name "name", at a glance you know that it is a variable related to the name.So be sure to name it meaningfully,Otherwise, the program is not readable.

Another point is about comments.Next to the method name of each method,Some comments should be added,At the same time, after a procedure is completed,You also need to understand the function of the program and how to operate it.Give a brief description.

As long as you do the above,This program is easy for others to read.Even after a long time,Read the program again at a glance.

  • Previous Workaround for NET Runtime Optimization Service 1101 error
  • Next Explain the type conversion and dynamic binding of Java polymorphic objects