---
title: "Self Encapsulate Field"
type: "refactoring-technique"
slug: "self-encapsulate-field"
url: "http://localhost:3000/en/self-encapsulate-field.md"
category: "Organizing Data"
description: "Problem: You use direct access to private fields inside a class. Solution: Create a getter and setter for the field, and use only them for accessing the field."
languages: ["java", "csharp", "php", "typescript"]
---
# Self Encapsulate Field

> Problem: You use direct access to private fields inside a class. Solution: Create a getter and setter for the field, and use only them for accessing the field.

## Problem

You use direct access to private fields inside a class.

## Solution

Create a getter and setter for the field, and use only them for accessing the field.

## Why Refactor

Sometimes directly accessing a private field inside a class just isn’t flexible enough. You want to be able to initiate a field value when the first query is made or perform certain operations on new values of the field when they’re assigned, or maybe do all this in various ways in subclasses.

## Benefits

* _Indirect access to fields_ is when a field is acted on via access methods (getters and setters). This approach is much more flexible than _direct access to fields_.

  * First, you can perform complex operations when data in the field is set or received. _Lazy initialization_ and _validation of field values_ are easily implemented inside field getters and setters.
  * Second and more crucially, you can redefine getters and setters in subclasses.
* You have the option of not implementing a setter for a field at all. The field value will be specified only in the constructor, thus making the field unchangeable throughout the entire object lifespan.

## How to Refactor

1. Create a getter (and optional setter) for the field. They should be either `protected` or `public`.
2. Find all direct invocations of the field and replace them with getter and setter calls.
## Relations

**Helps you do**

- [Duplicate Observed Data](/en/duplicate-observed-data.md)
- [Replace Type Code with Subclasses](/en/replace-type-code-with-subclasses.md)
- [Replace Type Code with State/Strategy](/en/replace-type-code-with-state-strategy.md)

**Similar techniques**

- [Encapsulate Field](/en/encapsulate-field.md)

## Code Examples

### java

```java
// Before
class Range {
  private int low, high;
  boolean includes(int arg) {
    return arg >= low && arg <= high;
  }
}

// After
class Range {
  private int low, high;
  boolean includes(int arg) {
    return arg >= getLow() && arg <= getHigh();
  }
  int getLow() {
    return low;
  }
  int getHigh() {
    return high;
  }
}
```

### csharp

```csharp
// Before
class Range 
{
  private int low, high;
  
  bool Includes(int arg) 
  {
    return arg >= low && arg <= high;
  }
}

// After
class Range 
{
  private int low, high;
  
  int Low {
    get { return low; }
  }
  int High {
    get { return high; }
  }
  
  bool Includes(int arg) 
  {
    return arg >= Low && arg <= High;
  }
}
```

### php

```php
// Before
private $low;
private $high;

function includes($arg) {
  return $arg >= $this->low && $arg <= $this->high;
}

// After
private $low;
private $high;

function includes($arg) {
  return $arg >= $this->getLow() && $arg <= $this->getHigh();
}
function getLow() {
  return $this->low;
}
function getHigh() {
  return $this->high;
}
```

### typescript

```typescript
// Before
class Range {
  private low: number
  private high: number;
  includes(arg: number): boolean {
    return arg >= low && arg <= high;
  }
}

// After
class Range {
  private low: number
  private high: number;
  includes(arg: number): boolean {
    return arg >= getLow() && arg <= getHigh();
  }
  getLow(): number {
    return low;
  }
  getHigh(): number {
    return high;
  }
}
```

