Because I used to format C # code before learning Java, that is
Figure 1The first code format, but when I look at the second code format, I feel that the code is messy and I cannot find the "{}" symmetric feeling. The shortcut for eclipse to automatically format the code is
CTRL + Shift + F, The first code style is achieved by setting eclipse.
The procedure is as follows:
Step 1:Start eclipse → Project → Properties → Java
Code style → Formatter →
Press
Figure 2In red.
Figure 2
Step 2:Follow
Figure 3In red.
Figure 3Step 3:The second step will pop upFigure 4Window. Select the braces tab, and then followFigure 4In red.
To set the class, the first "{" in the method, in the same row of Class Name and method name, must be in:
Windows-> prrferences-> JAVA-> code style-> formatter-> edit-> in braces, all settings are set to sameline. to display them in a new row, set them to next line.Note:Array initializer can remain unchanged as the default value "sameline". Otherwise, the line breaks at the Red 3 position in the figure, which is not nice.
Step 4:Open eclipse→ Window→Preferences→Java→Codestyle→Formatter→PressFigure 5In red.
Figure 5In addition, custom eclipse code specification templates
(1)
How to customize the code template in Eclipse:
Eclipse has built-in code formatting programs and templates. You can also create your own templates: 1. Select "Windows"-> "Preferences" in the menu bar 』.
2. Expand "code style" under "Java" in the left Tree of the displayed dialog box 』.
3. There are two items: "formatter" and "code template 』.
"Formatter" is mainly used to set the code format. The command used is: Ctrl + Shift + F.
For example, line feed and indentation. The format of the automatically generated code is set in "code template". The command is Alt + shinf + J.
For example:
Class file;
Setter, Getter, constructor, and other comments.
4.
Set "formatter": the built-in eclipse template cannot be exported. You need to create a new one. Enter the Template Name and select the file for initialization settings. This part mainly sets the format of the formatted code, including indentation, blank lines, and curly arc. Let's take a look at what you like. Click Export to save the file as a. xml file.
5.
There is nothing to set the "Code Template". It mainly sets the annotation formats of automatically generated methods, such as the comments of the getter/setter method. It can contain some variables that can be automatically filled with the corresponding content, such as name and parameter name. After setting, click Export all to save it as a. xml file.
6.
Click the import button to select the correct configuration file. Select the format under "source code" in the menu bar, or use the shortcut Ctrl + Shift + F to format the code. OK! (2)
Template-related commands:
2.1
CTRL + Shift + F
Use eclipse to create a template and format the code --> but do not automatically add comments.
2.2
Add the field/method annotation of a class or other element:
In the target row or method, press Alt + shinf + J. (You do not need to select a row or method body)
(Premise: In the code template, select the last line: automatically add comments for new methods and types)
Otherwise, when generating classes or getter and setter, You must select generate comments in the corresponding navigation panel.
2.3
CTRL +/: Add // comment
CTRL + shinf +/: Add block comment
CTRL + shinf + \: Remove block comments (3)
Use a template to generate an instance:
/**
* Project name: testselenium
* Package name: COM. formatter
* File name: person. Java
* Created time: 2010-2-9
* Copyright: Copyright (c) 2009 ~ 2010 Mmm All Rights Reserved.
*/
Package com. formatter ;/**
* Class descripton goes here.
*
* @ Version [mmm. Core. MM-V200M001, 2009-11-09]
* @ Author 9mmm31
*/
Public class person
{
/** Comment for <code> Number </code> */
Public static int number = 1;/** comment for <code> name </code> */
Public string name;/** comment for <code> age </code> */
Protected string age;/** comment for <code> password </code> */
Private string password ;/**
* Constructor
* @ Param number
*/
Public Person (INT number)
{
// Todo auto-generated constructor stub
}/**
* @ Return the name.
*/
Public String getname ()
{
Return name;
}/**
* @ Param name the name to set.
*/
Public void setname (string name)
{
If (name! = NULL)
{
This. Name = Name;
}
Else
{
This. Name = "";
}
}/**
* @ Return the age.
*/
Public String getage ()
{
Return age;
}/**
* @ Param age the age to set.
*/
Public void setage (string age)
{
This. Age = age;
}/**
* @ Return the password.
*/
Public String GetPassword ()
{
Return password;
}/**
* @ Param password the password to set.
*/
Public void setpassword (string password)
{
This. Password = password;
}
}
Code specifications:
Class Declaration Order:
1. Declare the variable static-> Public-> protected-> private.
2. Declare the constructor.
3. Declare the method.
4. To set the class, the first "{" in method, in the same row of Class Name and method name, it must be in:
Windows-> prrferences-> JAVA-> code style-> formatter-> edit-> in braces, all settings are set to sameline. to display them in a new row, set them to next line.