从构造函数中获取变量

时间:2017-05-07 14:18:20

标签: java

我使用此代码验证文件的数字符号,构造函数打印文件的上下文但我想知道如何将print保存为变量,因为构造函数仅使用" build&# 34;对象,这是代码:

public class VerifyMessage {
    private List<byte[]> list;

    @SuppressWarnings("unchecked")
    // The constructor of VerifyMessage class retrieves the byte arrays from the File and prints the message only if the signature is verified.
    public VerifyMessage(String filename, String keyFile) throws Exception {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
        this.list = (List<byte[]>) in.readObject();
        in.close();

       System.out.println(verifySignature(list.get(0), list.get(1), keyFile) ? "VERIFIED MESSAGE" + "\n----------------\n" + new String(list.get(0)) : "Could not verify the signature.");      
    }

我怎样才能&#34;保存&#34; System.out.println作为全局成员构造函数之外的String变量? 提前谢谢

2 个答案:

答案 0 :(得分:2)

您的课程已包含列表字段。

与此类似,您声明一个字符串字段 - 然后您只需将当前正在打印的值分配给该字段。

答案 1 :(得分:0)

public class VerifyMessage {
private List<byte[]> list;

Object variable = null;


public VerifyMessage(String filename, String keyFile) throws Exception {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
    this.list = (List<byte[]>) in.readObject();
    in.close();


    this.variable = this.verifySignature(list.get(0), list.get(1), keyFile);
}


private Object verifySignature(byte[] bs, byte[] bs2, String keyFile) {

    //
    return null;
}

}