Discuss Java source file declaration rules and programming style

  • 2020-04-01 04:10:08
  • OfStack

Declaration rules for Java source files
Pay special attention to these rules when defining multiple classes in a single source file, as well as import and package statements:
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, if the class name of the public class in the source file is Employee, the source file should be named employee.java.
If a class is defined in a package, the package statement should be on the first line of the source file.
If the source file contains import statements, 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 and package statements are valid for all classes defined in the source file. You cannot give different package declarations to different classes in the same source file.
Classes have several levels of access, and there are different types of classes: abstract classes, final classes, and so on. These will be covered in subsequent chapters.
In addition to the types mentioned above, Java also has some special classes, such as inner classes and anonymous classes.
A simple example

In this example, we create two classes Employee and EmployeeTest, placed in packages p1 and p2, respectively.

The Employee class has four member variables, name, age, designation, and salary. This class explicitly declares a constructor that takes only one argument.

In Eclipse, create a package named p1, create a class named Employee in the package, and copy the following code to the source file:


package p1;
public class Employee{
 String name;
 int age;
 String designation;
 double salary;
 //The constructor of the 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 following is the EmployeeTest class, which creates two Employee objects and invokes the method to set the value of the variable.

Create another package in Eclipse, name it p2, create a class in the package, name it EmployeeTest, and copy the following code to 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 the member methods of both 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 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 the programming style
Although the code style does not affect the running of the program, it is very important to the readability of the program. Write their own procedures to let others understand, first in the typography to be very careful.

In fact, everyone's programming style, each software development company's programming style is different. The program code written by a person, should be able to let others understand, even after a long time, oneself also want to understand, otherwise the program became a dead program.

Programming style refers to the format of programming, so that the program looks very hierarchical. Here are some examples of the importance of a 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 layout of the above section look comfortable and layered? Do you know the architecture at a glance? The key here is indentation, which can also be called a skip.

The code above is indented: "public class math" is the top, followed by the mian() method with four Spaces, the mian() method with eight Spaces, and the print() method with four more Spaces. So the ownership of the program is very clear. The mian() method belongs to the math class, the rest belongs to the main() method, and the code snippet in the print() method belongs to this method. The rule is that code with more space is subordinate to code with less space.

I recommend TAB indentation, and most editors (such as Eclipse) support custom TAB Spaces, typically four.

In addition to indentation, blank lines are also necessary. First 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 program segment above has a blank line between the print() method and the view() method to distinguish the different modules. The print() method does not do the same thing as the view() method, so use empty lines to separate them, which makes the program more readable.

Also, you need to pay attention to the naming of methods or properties. These names should have meaning and should be regular. Don't just use generic variables like "a" and "b". The "print" above, which other programmers know at a glance, is a function of printing or output. Another example: the variable name" name", at a glance to know that the name of the variable. So, make sure the name is meaningful, otherwise the program is not readable.

There's one more thing about comments. A few comments should be added next to the method name for each method, and a brief description of what the program does and how to do it should be made after the program is finished.

As long as you do the above, the program is easy for others to read. Even if you read the program after a long time, it will be clear.


Related articles: