2016-05-30 53 views
1

我在查找答案时发现了类似的问题,但是,他们都没有响应我的请求。无法初始化空白最终字段

我创建了一个模型,“会议”

@Entity 
@Scope("prototype") 
@Table(name="MEETINGS") 
public class Meeting { 



@Id @GeneratedValue(strategy=GenerationType.IDENTITY) 
private Integer id; 

@NotEmpty 
@Column(name="name", nullable=false) 
private String name; 

@NotEmpty 
@Column(name="description", nullable=false) 
private String description; 

@NotEmpty 
@Column(name="allowStartStopRecording", nullable=false) 
private String allowStartStopRecording; 

@NotEmpty 
@Column(name="autoStartRecording", nullable=false) 
private String autoStartRecording; 

@NotEmpty 
@Column(name="record", nullable=false) 
private String record; 

@NotEmpty 
@Column(name="voiceBridge", nullable=false) 
private String voiceBridge; 

public Integer getId() { 
    return id; 
} 

public void setId(Integer id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getAllowStartStopRecording() { 
    return allowStartStopRecording; 
} 

public void setAllowStartStopRecording(String allowStartStopRecording) { 
    this.allowStartStopRecording = allowStartStopRecording; 
} 

public String getAutoStartRecording() { 
    return autoStartRecording; 
} 

public void setAutoStartRecording(String autoStartRecording) { 
    this.autoStartRecording = autoStartRecording; 
} 

public String getRecord() { 
    return record; 
} 

public void setRecord(String record) { 
    this.record = record; 
} 

public String getVoiceBridge() { 
    return voiceBridge; 
} 

public void setVoiceBridge(String voiceBridge) { 
    this.voiceBridge = voiceBridge; 
} 


} 

,并试图创建一个DAO“MeetingDAO”,

public interface MeetingDAO { 

Meeting meeting; 

String createMeeting(); 

String deleteMeeting(); 

String getMeetings(); 

String updateMeeting(); 
} 

我得到了以下错误:在空白的最终场会议可能没有已经在包含会议会议的路线中初始化;

预先感谢您。

回答

1

请看看:https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html

据说:

接口中定义的所有常量都是public, 静态和决赛。再一次,你可以省略这些修饰符。

所以在接口MeetingDAO变量声明Meeting meeting;结果是:public static final Meeting meeting;你离开它未初始化。这是您得到的原因:blank final field meeting may not have been initialized编译错误。

再就是你的界面MeetingDAO可能看起来像以下(其它变化也是可能的):

import java.util.List; 

public interface MeetingDAO { 

    void createMeeting(Meeting meeting); 

    Meeting getMeeting(long meetingId); 

    void deleteMeeting(long meetingId); 

    List<Meeting> getMeetings(); 

    void updateMeeting(Meeting meeting); 
} 

你觉得呢?

+0

这正是我试图做 和它的工作 – Najoua

3

您正在尝试在界面中声明成员变量:

public interface MeetingDAO { 

    Meeting meeting; 

在接口不能声明成员变量。会员meeting自动生成public static final(即一个常数),并且您没有初始化它。

不要在MeetingDAO接口中声明meeting。如果您需要声明类型为Meeting的成员变量,请在实现该接口的类中执行此操作。但坦率地说,如果MeetingDAO的实现有一个Meeting类型的成员变量,那么你可能根本就不应该有这个成员变量。 (另外,为什么界面中的所有方法都返回String而没有使用任何参数?这看起来不合逻辑)。

+0

是啊,你是对的 我犯了大错,我没有注意这个 那么,对于返回类型,我只是在考验,他们将有不同的类型和参数 – Najoua