Purpose of a constructor in Java?
问题 What is the purpose of a constructor? I've been learning Java in school and it seems to me like a constructor is largely redundant in things we've done thus far. It remains to be seen if a purpose comes about, but so far it seems meaningless to me. For example, what is the difference between the following two snippets of code? public class Program { public constructor () { function(); } private void function () { //do stuff } public static void main(String[] args) { constructor a = new