You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Lombok创建指定字段构造器遇报错:如何生成仅两字段构造器?

Fixing "The constructor Category(String, String) is not visible" with Lombok

Got it, let's break down how to generate a constructor for exactly two fields in your Category class while using Lombok, and fix that visibility error you're seeing.

First, Understand the Root of the Error

That "not visible" message usually boils down to one of two issues:

  • The two-parameter constructor you're trying to use doesn't exist at all (Lombok didn't generate it as you expected)
  • The constructor does exist, but it's marked as private or package-private, so your App.java can't access it

Solution 1: Manually Write the Constructor (Simplest & Most Predictable)

Lombok plays perfectly nice with manually written code—you don't have to rely solely on annotations. If you only need a constructor for two specific fields, just write it out directly alongside your Lombok annotations:

import lombok.Data;
import java.util.List;

@Data // Generates getters, setters, toString, equals/hashCode, and a no-arg constructor
public class Category {
    // Fields you want in the constructor
    private String name;
    private String description;
    
    // Other fields
    private Long id;
    private List<Product> products;

    // Your custom two-field constructor
    public Category(String name, String description) {
        this.name = name;
        this.description = description;
    }
}

This works flawlessly—Lombok won't overwrite or conflict with your manual constructor. You can now call new Category("Books", "Fiction and non-fiction") in App.java without any visibility issues.

Solution 2: Use Lombok's @RequiredArgsConstructor (Auto-Generate the Constructor)

If you prefer to let Lombok handle constructor generation, use @RequiredArgsConstructor along with either final or @NonNull on the fields you want included. Lombok will automatically create a public constructor for all fields marked this way:

import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import java.util.List;

@Data
@RequiredArgsConstructor // Generates constructor for @NonNull/final fields
public class Category {
    // Mark fields to include with @NonNull
    @NonNull
    private String name;
    @NonNull
    private String description;
    
    // Other fields (not included in constructor)
    private Long id;
    private List<Product> products;
}

Or if you prefer using final instead of @NonNull:

import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.util.List;

@Data
@RequiredArgsConstructor
public class Category {
    private final String name;
    private final String description;
    
    private Long id;
    private List<Product> products;
}

Both approaches will generate a public Category(String, String) constructor that your App.java can access freely.

How to Avoid the Visibility Trap

If you still get the "not visible" error after trying these, double-check:

  • You didn't set the access parameter on @RequiredArgsConstructor to something restrictive like AccessLevel.PRIVATE (the default is AccessLevel.PUBLIC, which is what you want)
  • You're not mixing conflicting annotations (e.g., @NoArgsConstructor alone won't generate your two-arg constructor—you need to add the custom constructor or @RequiredArgsConstructor)

内容的提问来源于stack exchange,提问作者Jeff Cook

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:26:51