Reputation: 765
I am able to call MessageDigest as a local variable in main() method of my code, but whenever I try creating a field variable of MessageDigest in a class, and try initializing it in a constructor, it throws a NoSuchAlgorithm exception.
My gut feeling is that we just cannot instantiate MessageDigest objects like that. Is that true?
Basically, this is what I did. I want to use this as a field variable, and not a local variable.
Upvotes: 2
Views: 406
Reputation: 310926
You are correct. Because of that exception being declared to be thrown, the only way you can initialize a MessageDigest object as a member variable is via either a constructor thatbtheows that exception, or a constructor or an initializer that contains an appropriate try/catch block. Otherwise it won't compile.
Upvotes: 1