Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
Point a = new Point(5);
Point b = new Point();
System.out.println("end");
}
}
class Point {
Point(int x) {
System.out.print(x + " ");
}
}It does not compile
- Expecting
5 end: once a class declares any constructor, Java adds no default one, sonew Point()does not compile. - Expecting
5 0 endor a run-time error: Java does not fill in missing arguments, and the compiler rejects the call before anything runs.
- How would you change
Pointso thatnew Point()compiles and builds a point withxequal to 0? - Which constructor does Java add to a class that declares none?