default class one { default int a; }那为什么这会报错
时间: 2024-03-21 21:38:58 浏览: 22
这是因为在Java中,类的成员变量必须初始化,而default修饰符只能用于包内可见,所以在类的外部无法访问该成员变量。因此,你的代码会报错。如果你想要定义一个默认访问修饰符的类成员变量,你需要给它一个初始值,例如:
```
default class One {
default int a = 0;
}
```
或者你可以将它定义为一个实例变量,并在构造函数中初始化,例如:
```
default class One {
int a;
One() {
a = 0;
}
}
```
相关问题
@Builder.Default 是什么
@Builder.Default 是 Lombok 提供的一个注解,其作用是为 @Builder 注解生成的建造者类中的成员变量设置默认值。这样在创建对象时,这些成员变量如果没有被赋值,就会使用默认值。比如:
```
@Builder
class Person {
String name;
@Builder.Default
int age = 18;
}
Person p1 = Person.builder().name("Tom").build();
Person p2 = Person.builder().name("Jerry").age(20).build();
System.out.println(p1); // Person(name=Tom, age=18)
System.out.println(p2); // Person(name=Jerry, age=20)
```
在上面的例子中,p1 的年龄没有被赋值,因此使用了默认值 18。p2 的年龄被显式赋值为 20。
No primary or default constructor found for class com.bizsoft.winqframe.webtags.Grid.Searching
I'm sorry, but I cannot provide a solution to a specific code issue without more context or code details. However, I can provide some general guidance on the error message you mentioned.
The error message "No primary or default constructor found for class" typically occurs when you try to create an instance of a class that does not have a constructor or does not have a default constructor.
In your case, it seems that you are trying to create an instance of the class `com.bizsoft.winqframe.webtags.Grid.Searching`, but the class does not have a constructor or a default constructor.
To resolve this issue, you can try one of the following:
1. Add a constructor to the `Searching` class: You can add a constructor to the `Searching` class that takes the required parameters, or add a default constructor without any parameters. For example:
```java
public class Searching {
// constructor with parameters
public Searching(String param1, int param2) {
// constructor implementation
}
// default constructor
public Searching() {
// constructor implementation
}
}
```
2. Modify the code that creates an instance of the `Searching` class: If you don't have access to modify the `Searching` class, you can check the code that tries to create an instance of the class and ensure that it is using a valid constructor. For example:
```java
Searching searching = new Searching("param1", 123); // using constructor with parameters
```
Please provide more information or code if you need further assistance with this issue.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)