# Make SerialVersionUID Static Final

# Description

Adds the modifiers static and final to SerialVersionUid long variables when they are absent.

# Benefits

For serializable classes the SerialVersionUid should be written explicitly. If the UID changes, that can lead to errors (for example, an object not being able to be deserialized). To avoid those errors, apply this rule.

# Tags

# Code Changes

Pre

import java.io.Serializable;

public class TestSerialVersionUidRule {
    private class Test01 implements Serializable {
        /**
         *
         */
        private long serialVersionUID = 1L;
        private long otherField;
        private Object objectField;
    }
    private class Test02 implements Serializable {
        /**
         *
         */
        private long testLong, serialVersionUID = 1L;
    }
}

Post

import java.io.Serializable;

public class TestSerialVersionUidRule {
    private class Test01 implements Serializable {
        /**
         *
         */
        private static final long serialVersionUID = 1L;
        private long otherField;
        private Object objectField;
    }
    private class Test02 implements Serializable {
        private static final long serialVersionUID = 1L;
        /**
         *
         */
        private long testLong;
    }
}

Use a Java Refactoring Tool

No license required

You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.

System-wide Refactoring

Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.

a drawn cute bird pointing at a graph that shows positive results

# Properties

Property Value
Rule ID SerialVersionUID
First seen in jSparrow version 1.0.0
Minimum Java version 1.1
Remediation cost 5 min
Links