EmptyLineSeparator

Since Checkstyle 5.8

Description

Checks for empty line separators before package, all import declarations, fields, constructors, methods, nested classes, static initializers and instance initializers.

Checks for empty line separators before not only statements but implementation and documentation comments and blocks as well.

ATTENTION: empty line separator is required between token siblings, not after line where token is found. If token does not have a sibling of the same type, then empty line is required at its end (for example for CLASS_DEF it is after '}'). Also, trailing comments are skipped.

Properties

name description type default value since
allowMultipleEmptyLines Allow multiple empty lines between class members. boolean true 6.3
allowMultipleEmptyLinesInsideClassMembers Allow multiple empty lines inside class members. boolean true 6.18
allowNoEmptyLineBetweenFields Allow no empty line between fields. boolean false 5.8
tokens tokens to check subset of tokens PACKAGE_DEF , IMPORT , STATIC_IMPORT , CLASS_DEF , INTERFACE_DEF , ENUM_DEF , STATIC_INIT , INSTANCE_INIT , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . PACKAGE_DEF , IMPORT , STATIC_IMPORT , CLASS_DEF , INTERFACE_DEF , ENUM_DEF , STATIC_INIT , INSTANCE_INIT , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . 5.8

Examples

To configure the default check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator"/>
  </module>
</module>
        

Example of declarations without empty line separator:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;
import java.io.Serializable;
// violation 2 lines above ''package' should be separated from previous line'
// violation 2 lines above ''import' should be separated from previous line'

class Example1 {

  int var1 = 1;
  int var2 = 2; // violation ''VARIABLE_DEF' should be separated from previous line'


  int var3 = 3;


  void method1() {}
  void method2() { // violation ''METHOD_DEF' should be separated from previous line'
    int var4 = 4;


    int var5 = 5;
  }
}
        

To check empty line before VARIABLE_DEF and METHOD_DEF:

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator">
      <property name="tokens" value="VARIABLE_DEF, METHOD_DEF"/>
    </module>
  </module>
</module>
        

Example:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;
import java.io.Serializable;

class Example2 {

  int var1 = 1;
  int var2 = 2; // violation ''VARIABLE_DEF' should be separated from previous line'


  int var3 = 3;


  void method1() {}
  void method2() { // violation ''METHOD_DEF' should be separated from previous line'
    int var4 = 4;


    int var5 = 5;
  }
}
        

To allow no empty line between fields:

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator">
      <property name="allowNoEmptyLineBetweenFields" value="true"/>
    </module>
  </module>
</module>
        

Example:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;
import java.io.Serializable;
// violation 2 lines above ''package' should be separated from previous line'
// violation 2 lines above ''import' should be separated from previous line'

class Example3 {

  int var1 = 1;
  int var2 = 2;


  int var3 = 3;


  void method1() {}
  void method2() { // violation ''METHOD_DEF' should be separated from previous line'
    int var4 = 4;


    int var5 = 5;
  }
}
        

To disallow multiple empty lines between class members:

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator">
      <property name="allowMultipleEmptyLines" value="false"/>
    </module>
  </module>
</module>
        

Example:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;
import java.io.Serializable;
// violation 2 lines above ''package' should be separated from previous line'
// violation 2 lines above ''import' should be separated from previous line'

class Example4 {

  int var1 = 1;
  int var2 = 2; // violation ''VARIABLE_DEF' should be separated from previous line'


  int var3 = 3; // violation ''VARIABLE_DEF' has more than 1 empty lines before'


  void method1() {} // violation ''METHOD_DEF' has more than 1 empty lines before'
  void method2() { // violation ''METHOD_DEF' should be separated from previous line'
    int var4 = 4;


    int var5 = 5;
  }
}
        

To disallow multiple empty lines inside constructor, initialization block and method:

<module name="Checker">
  <module name="TreeWalker">
    <module name="EmptyLineSeparator">
      <property name="allowMultipleEmptyLinesInsideClassMembers"
                value="false"/>
    </module>
  </module>
</module>
        

The check is valid only for statements that have body: CLASS_DEF , INTERFACE_DEF , ENUM_DEF , STATIC_INIT , INSTANCE_INIT , METHOD_DEF , CTOR_DEF .

Example of declarations with multiple empty lines inside method:

///////////////////////////////////////////////////
//HEADER
///////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.checks.whitespace.emptylineseparator;
import java.io.Serializable;
// violation 2 lines above ''package' should be separated from previous line'
// violation 2 lines above ''import' should be separated from previous line'

class Example5 {

  int var1 = 1;
  int var2 = 2; // violation ''VARIABLE_DEF' should be separated from previous line'


  int var3 = 3;


  void method1() {}
  void method2() { // violation ''METHOD_DEF' should be separated from previous line'
    int var4 = 4; // violation 'There is more than 1 empty line after this line'


    int var5 = 5;
  }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.whitespace

Parent Module

TreeWalker