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 constructor();
}
}
This is how we were taught do to things for assignments, but wouldn't the below do the same deal?
public class Program {
public static void main(String[] args) {
function();
}
private void function() {
//do stuff
}
}
The purpose of a constructor escapes me, but then again everything we've done thus far has been extremely rudimentary.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…