I don’t understand this form of static variable declaration

Asked

Viewed 38 times

3

Why this statement seems neither method nor attribute?

static{

}

this is the original code

private static MessageDigest md5 = null;

        static {
            try {
                md5 = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException ex) {
                ex.printStackTrace();
            }
        }

Prof. couldn’t explain it.

1 answer

1


This statement is used within a class when we want to execute some code snippet on class startup, which includes assigning something to a variable static.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.