Given the following code:

public class Foo {
    private Foo() {
        return this;            // 1
    public static Foo get() {
        return new Foo();       // 2
    public static void main(String[] args) {
        Foo foo1 = get();       // 3
        Foo foo2 = new Foo();   // 4
Will there be a compilation error and if so, in what lines?
Code will not compile.
There will be a compilation error in line 1 - in the constructor in the return statement it's not allowed to specify the return value.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Sign Up Now
or Subscribe for future quizzes