Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Condition 1:</p> <p>From Oracle Java Tutorial: </p> <p>From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an <strong>explicit constructor invocation</strong>.</p> <p>If present, the invocation of another constructor <strong>must be the first line in the constructor</strong>.</p> <p>So you can't create a File reference pointing to null before explicit constructor invocation. </p> <p>e.g.: below code throws compilation error: call to this must be first statement in constructor </p> <p>public class Hello {</p> <pre><code>public Hello() { File file = null; this(file); System.out.println("hello - 1"); } public Hello(File file) { System.out.println("hello - 2"); } public Hello(String str) { System.out.println("hello - 3"); } public static void main(String[] args) { Hello h = new Hello(); } </code></pre> <p>}</p> <p>Condition 2: </p> <p>Check your code, do you have more than one constructor which accepts one input parameter of type Object (in general)?</p> <p>e.g., below code throws compilation error: reference to Hello is ambiguous, both constructor Hello(File) in Hello and constructor Hello(String) in Hello match</p> <p>public class Hello {</p> <pre><code>public Hello() { this(null); System.out.println("hello - 1"); } public Hello(File file) { System.out.println("hello - 2"); } public Hello(String str) { System.out.println("hello - 3"); } public static void main(String[] args) { Hello h = new Hello(); } </code></pre> <p>}</p> <p>To avoid ambiguity, typecast the null as below:</p> <p>public class Hello {</p> <pre><code>public Hello() { this((File)null); System.out.println("hello - 1"); } public Hello(File file) { System.out.println("hello - 2"); } public Hello(String str) { System.out.println("hello - 3"); } public static void main(String[] args) { Hello h = new Hello(); } </code></pre> <p>}</p> <p>Hope,this helps</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload