Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Yes—Java classes can hold and expose data without JavaBean-style getters and setters, but a framework must use a different access strategy. Hibernate can map fields directly, Jackson can use fields or constructor parameters, and Java records provide immutable component accessors. The key distinction is that a Java field is not automatically a JavaBeans property: every tool that reads, writes, validates, or binds the data must support the strategy you choose.

What “property” means in Java

A field stores state in a class. A JavaBeans property is a conventional interface to state, usually discovered through methods such as getName(), isActive(), and setName(...). JavaBeans tools may also recognize a read-only property with only a getter or a write-only property with only a setter. A private field with no matching accessor is still a field, but a JavaBeans-only tool will not automatically discover it as a property.

Frameworks can define their own property model. They may inspect fields, call accessor methods, or use constructor parameters. Reflection also allows infrastructure to obtain a Field object for dynamic field access, subject to Java’s encapsulation, module, and access checks. See the Java Field API.

Persist private fields with Hibernate

Hibernate supports both property-based access, which uses JavaBean-style methods, and field-based access, which reads and writes mapped instance fields directly. With field-based access, an entity can keep its fields private and omit accessors that the application does not need. Hibernate’s guide specifically notes that a version field can remain hidden from callers while still being used for optimistic concurrency control. See the Hibernate access-strategy documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Example: field access

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Version;

@Entity
public class Account {
    @Id
    private Long id;

    private String name;

    @Version
    private long version;

    protected Account() {
        // For ORM construction
    }

    public Account(Long id, String name) {
        this.id = id;
        this.name = name;
    }
}

Because @Id is placed on a field, the mapping uses field access by default. The entity has no JavaBean getters or setters; Hibernate accesses the mapped fields. The example uses Jakarta Persistence imports, so the imports must match the persistence API used by the project. Consult the guide for the Hibernate version in use for version-specific mapping and entity requirements.

Annotation placement matters

Hibernate commonly infers the default access strategy from where the identifier mapping is placed. Put mapping annotations on fields to select field access; put them on accessor methods to select property access. Mixing placements without deliberately configuring access can lead to confusing mappings or inconsistent behavior. Before moving annotations, check the entity’s intended strategy and the rules for the chosen Hibernate and Jakarta Persistence versions; the placement is not merely a formatting choice.

Use fields or constructors with Jackson

Jackson treats a property as a logical data item that can be represented by an accessor, a field, or a constructor parameter. As a result, serialization and deserialization need not rely on setters: a mapper can be configured to detect fields, or deserialization can use a suitable constructor or creator. The details depend on the Jackson version and mapper configuration, so verify the actual behavior in the application rather than assuming private fields are visible by default. The Jackson Databind project documentation describes the library’s data-binding capabilities.

For an immutable DTO, a constructor-based design can be preferable to setters: the constructor receives the data and establishes the object’s state once. For a mutable DTO using fields, configure field visibility intentionally and ensure the selected deserialization path can populate the fields. Do not assume that a configuration suitable for one Jackson version or mapper applies unchanged to another.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use records for immutable data

A record is an option when the data should be immutable after construction. Its components define the record’s state, and the language generates component accessors, such as name(), along with a canonical constructor. These are methods, but they are not JavaBean-style getName() and setName(...) methods. Records therefore avoid conventional getter/setter boilerplate; they are not a way to expose mutable fields directly.

public record Person(String name, int age) {}

Code using the record reads values with person.name() and person.age(). A consumer expecting getName() or a setter may not treat the record as it treats a traditional JavaBean, so check compatibility with the frameworks and libraries involved.

Choose an access strategy that every consumer supports

Approach How state is accessed Useful when Main consideration
JavaBeans accessors Conventional get, is, and set methods Tools rely on JavaBeans introspection, or methods need to enforce validation and invariants Mutable objects need setters where updates are required
Hibernate field access Hibernate reads and writes mapped fields An entity should omit boilerplate accessors or keep persistence state private Field access is framework-dependent; mapping annotation placement can select the strategy
Jackson field or constructor access The mapper uses fields or constructor parameters Serialization needs no conventional getters, or deserialization should construct an immutable DTO Visibility and creator behavior depend on mapper settings and Jackson version
Java record Generated component accessors and a canonical constructor Data should be immutable and record-aware consumers are available Component accessors are named name(), not getName(); records do not provide setters

Before removing accessors, check each consumer of the class: ORM, JSON mapper, validation framework, UI or data binding, application code, and tests. A strategy that works for Hibernate does not automatically work for a JavaBeans-based binding library. Field or constructor access can reduce boilerplate, while accessors remain useful when they enforce rules or when interoperability depends on JavaBean naming.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common failure modes and checks

  • An ORM stops mapping an entity as expected: Check where @Id and other mapping annotations are placed and which access strategy the entity uses.
  • Jackson omits a value or cannot populate it: Check field visibility, creator or constructor configuration, and the Jackson version and mapper settings.
  • A binding or introspection tool reports no property: Determine whether it supports fields or constructors; JavaBeans-only discovery expects accessor conventions.
  • Proxying or enhancement behaves differently: ORM proxying and bytecode enhancement can impose visibility or non-final-method constraints. Check the documentation for the selected Hibernate version before making classes or methods final.
  • Application code needs validation during updates: Consider keeping an explicit method that validates a change rather than exposing unrestricted field mutation or relying on persistence access to enforce application rules.

Field access is not a universal substitute for accessors. It is appropriate when the framework explicitly supports it and the object’s other consumers do too; otherwise, use JavaBean methods or a constructor-based design that matches those consumers.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.