Achat Cialis achat sildigra prof sublingual cialis professional 20 mg sildisoft acheter viagra pharmacie acheter propecia en ligne generique cialis pharmacie tadacip tadalafil silagra 25 achat levitra sans ordonnance acheter kamagra pas cher caverta en ligne kamagra liquide cialis soft tabs kamagra soft pharmacie

Constructor call must be the first statement in a constructor

From JavaErrors

Jump to: navigation, search

You will get the error

 Constructor call must be the first statement in a constructor

if you try to set up some variables and then call a constructor. You need to construct the object first, then modify it.

WRONG

 public class Foo {
   int i;
   public Foo() {
     i = 2;
   }
   public Foo(int x) {    // fine
     this();
     this.i = x;
   }
   public Foo(int x, int y) {
     int z = x*y;
     this(z);             // not okay
   }
 }

RIGHT

 public class Foo {
   int i;
   public Foo() {
     i = 2;
   }
   public Foo(int x) {    
     this();
     this.i = x;
   }
   public Foo(int x, int y) {
     this(x*y);             // now okay
   }
 }

ALSO RIGHT

 public class Foo {
   int i;
   public Foo() {
     i = 2;
   }
   public Foo(int x) {    
     this();
     this.i = x;
   }
   private static int multiply(int x,int y) {
     return x*y;
   }
   public Foo(int x, int y) {
     this(Foo.multiply(x, y));             // also okay
   }
 }

Note that you can't call an instance method, since the object doesn't exist:

WRONG

 public class Foo {
   int i;
   public Foo() {
     i = 2;
   }
   public Foo(int x) {    
     this();
     this.i = x;
   }
   private int multiply(int x,int y) {     // this must be static for it to work
     return x*y;
   }
   public Foo(int x, int y) {
     this(multiply(x, y));             // not okay -- multiply can't work yet
   }
 }
Personal tools