Java code formatting in Eclipse settings method

Source: Internet
Author: User
Because before learning Java is accustomed to C # code formatting style, namely the following Figure 1The first code format, and look at the second code format when the code is very messy, always find "{}" symmetrical feeling. The shortcut to the Eclipse Automatic formatting code is ctrl+shift+f, the first style of code is achieved by setting eclipse.
set the steps as follows: First step:Start Eclipse→project→properties→java Code style→formatter→ Press Figure 2The red font of the step operation.
Figure 2
Step Two:According to Figure 3The red font step operation.

Figure 3 Step Three:After the second step will pop up Figure 4Window. Select the Braces tab at this time, and then follow Figure 4The red font step operation.

To set the first "{" In the Class,method, in the same row as the class name and method name, to:
Windows->prrferences->java->code style->formatter->edit->braces, all set to Sameline. To display on a new line, is set to next line.

Note:the Array initializer can remain as the default value unchanged "Sameline", otherwise the Red 3 position in the picture will be wrapped, it is not good to see.

Fourth Step:Open eclipse→window→preferences→java→codestyle→formatter→ Press Figure 5The red font step operation.

Figure 5







Also: Customizing the Eclipse Code specification template


(1)
How to customize the code templates in eclipse:
Eclipse has built-in code formatters and templates, or you can create your own templates:
1. In the menu bar, select Windows-〉 "Preferences".
2. In the left tree of the dialog box that appears, expand Code Style under Java.
3. Two of them, one is "Formatter" and the other is "code template".
"Formatter" is mainly the format of the code. The command to use is: ctrl+shift+f.
such as: line change, indentation, etc.,
The code template is set up in the format of auto-generated codes, using the command: Alt+shinf+j.
Such as:
The annotation at the beginning of the class file;
Setter and Getter methods, construction methods, and other related notes.

4.
Set "Formatter": Eclipse built-in templates cannot be exported, you need to create a new one. Enter the name of the template and select the file to initialize the settings. This section is mainly to format the formatted code, including indents, blank lines, curly brackets, and so on. The specific things in accordance with their own hobbies, you slowly determine it. Then click Export to Save as an. xml file.
5.
There is nothing to set "code template", which is mainly to set up annotation formats for automatically generated methods, such as comments for the Getter/setter method. Inside can include some variables, can be generated automatically fill the corresponding content, such as name, parameter name. After setting, click All Export, Save As. xml file.
6.
Use the time of course is to click the Import button, 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 command description:
2.1
CTRL + SHIFT + F
Use Eclipse to create templates and format code--> without automatically adding comments.
2.2
Add a class or other element Field/method Note:
In the target line, or in the target method, press ALT+SHINF+J. (No line or method body is selected)
(Prerequisite: In the code template, select the last line: Automatically add comments for new methods and types)
Otherwise, when you generate a class or generate a Getter,setter, you must select the build comment in the appropriate navigation panel.
2.3
ctrl+/: Add//Comment
ctrl+shinf+/: adding block annotations
Ctrl+shinf+\: Remove block annotations
(3)
Using templates 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 of 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;
}
}
Related Code Specification Description:
The declaration order of the class:
1. Declare the variable static->public->protected->private first.
2. Declare the construction method.
3. Declaration method.
4. To set the first "{" In Class,method, in the same row as the class name and method name, to:
Windows->prrferences->java->code style->formatter->edit->braces, all set to Sameline. To display on a new line, is set to next line.

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.