Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 475535

Summary: Encapsulate field refactoring also replaces a field assignment by the set method in the constructor.
Product: [Eclipse Project] JDT Reporter: Melina Mongiovi <melmongiovi>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 4.6   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard: stalebug

Description Melina Mongiovi CLA 2015-08-20 17:34:39 EDT
Applying the encapsulate field refactoring in the field A.f, the transformation replaces the field assignment f = 10 in the A constructor by the set method setF(10).

When you are using self-encapsulation you have to be careful about using the setting method in the constructor. Often it is assumed that you use the setting method for changes after the object is created, so you may have different behavior in the setter than you have when initializing.

Before Refactoring:

public class A {
    private int f;    
    public A() {
        f = 10;
    }
    int m(int x) {
        return f;
    }
}

Resulting program:

public class A {
    private int f;    
    public A() {
        setF(10);
    }
    int m(int x) {
        return getF();
    }
	private int getF() {
		return f;
	}
	private void setF(int f) {
		this.f = f;
	}
}
Comment 1 Eclipse Genie CLA 2019-11-28 14:51:24 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.