Please give me a reason for this.

Dear All,While calling a constructor from a method inside the same class. We normally get a compiler time error as "call to this must be the first statement in the constructor" Why is this so.
[227 byte] By [Hrithik_Sama] at [2007-11-26 14:54:52]
# 1

friend oh friend...

1 basic thing constructor is a routine which has to be called only @ the time of intantiating an Object (Instance of a class).

If u r trying to call the constructor inside the scope same instance it is a invalid operation.

U can very well create a new instance of the object and try calling it ....

Hope that makes ur answer clear

RahulSharnaa at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 2
Post the code where u got this error, I will help U
Gudalaa at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 3

/**

* implementing the concept of constructor and this keyword in use with it.

*/

class Flower

{

private int petals;

private String colors;

Flower()

{

//System.out.println("I am default");

this("Red",5);

System.out.println("I am default");

}

Flower(int petal)

{

System.out.println("I am with one arg");

System.out.println(petal);

}

Flower(String col)

{

System.out.println("I am with one arg");

System.out.println(col);

}

Flower(String col,int petal)

{

this(petal);

//this(col);

System.out.println("I am with two arg");

System.out.println(col);

System.out.println(petal);

}

private void disp()

{

this("green");

System.out.println("in method");

}

public static void main(String args[])

{

Flower obj=new Flower();

obj.disp();

}

}

Hrithik_Sama at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 4

with reference to my earlier post this has to cater your requirement...

public class Flower

{

private int petals;

private String colors;

public Flower()

{

//System.out.println("I am default");

this("Red",5);

System.out.println("I am default");

}

public Flower(int petal)

{

System.out.println("I am with one arg") ;

System.out.println(petal);

}

public Flower(String col)

{

System.out.println("I am with one arg") ;

System.out.println(col);

}

public Flower(String col,int petal)

{

this(petal);

//this(col);

System.out.println("I am with two arg");

System.out.println(col);

System.out.println(petal);

}

private void disp()

{

new Flower("green");

System.out.println("in method");

}

public static void main(String args[])

{

Flower obj = new Flower();

obj.disp();

}

}

RahulSharnaa at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 5

class Flower {

private int petals;

private String colors;

Flower() {

//System.out.println("I am default");

this("Red",5);

System.out.println("I am default");

}

Flower(int petal) {

this(null, petal);

System.out.println("I am with one arg");

System.out.println(petal);

}

Flower(String col) {

this(col, 0);

System.out.println("I am with one arg");

System.out.println(col);

}

Flower(String col,int petal) {

this.col = col;

this.petal = petal;

System.out.println("I am with two arg");

System.out.println(col);

System.out.println(petal);

}

private void disp() {

this("green");

System.out.println("in method");

}

public static void main(String args[]) {

Flower obj=new Flower();

obj.disp();

}

}

paulcwa at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 6
Mine works better.
paulcwa at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 7
Dear Rahul,basic thing constructor is a routine which has to be called only @ the time of intantiating an ObjectThis itself has clarified me. I am really thankfull. Take care.
Hrithik_Sama at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 8
Dear Paul,Thank you. Now i am clear.
Hrithik_Sama at 2007-7-8 8:43:20 > top of Java-index,Java Essentials,Java Programming...
# 9

@paulcw

Tried the same code....

this is what u get.....

D:\>javac Flower.java

Flower.java:21: cannot find symbol

symbol : variable col

location: class Flower

this.col = col;

^

Flower.java:22: cannot find symbol

symbol : variable petal

location: class Flower

this.petal = petal;

^

Flower.java:28: call to this must be first statement in constructor

this("green");

^

3 errors

D:\>javac Flower.java

Flower.java:21: cannot find symbol

symbol : variable col

location: class Flower

this.col = col;

^

Flower.java:22: cannot find symbol

symbol : variable petal

location: class Flower

this.petal = petal;

^

Flower.java:28: call to this must be first statement in constructor

this("green");

^

3 errors

corrected it to

class Flower {

private int petals;

private String colors;

private int col;

private int petal;

Flower() {

//System.out.println("I am default");

this("Red",5);

System.out.println("I am default");

}

Flower(int petal) {

this(null, petal);

System.out.println("I am with one arg");

System.out.println(petal);

}

Flower(String col) {

this(col, 0);

System.out.println("I am with one arg");

System.out.println(col);

}

Flower(String col,int petal) {

this.col = col;

this.petal = petal;

System.out.println("I am with two arg");

System.out.println(col);

System.out.println(petal);

}

private void disp() {

this("green");

System.out.println("in method");

}

public static void main(String args[]) {

Flower obj=new Flower();

obj.disp();

}

}

this is what U get if you use the same thing

D:\>javac Flower.java

Flower.java:23: incompatible types

found: java.lang.String

required: int

this.col = col;

^

Flower.java:30: call to this must be first statement in constructor

this("green");

^

2 errors

Hope U got the reason behind that....

@Hrithik

How about my duke dollars friend ?

REGARDS,

RaHuL

RahulSharnaa at 2007-7-8 8:43:21 > top of Java-index,Java Essentials,Java Programming...
# 10
I guess thats a typo
@a at 2007-7-8 8:43:21 > top of Java-index,Java Essentials,Java Programming...
# 11

That'll show me to post without testing.

@OP: try this:

class Flower {

private int petals;

private String color;

private static final boolean TRACE = false;

Flower() {

this("Red",5);

if (TRACE)

System.out.println("I am no-arg constructor");

}

Flower(int petal) {

this(null, petal);

if (TRACE) {

System.out.println("I am one arg (int) constructor");

System.out.println(petal);

}

}

Flower(String col) {

this(col, 0);

if (TRACE) {

System.out.println("I am one arg (string) constructor");

System.out.println(col);

}

}

Flower(String col,int petal) {

this.color = col;

this.petals = petal;

if (TRACE) {

System.out.println("I am with two arg");

System.out.println(col);

System.out.println(petal);

}

}

public String toString() {

return "A " + (color != null ? color : "colorless")

+ " flower with " + petals + " petals.";

}

public static void main(String args[]) {

Flower obj = new Flower();

System.out.println(obj);

obj = new Flower("Green");

System.out.println(obj);

obj = new Flower(64);

System.out.println(obj);

obj = new Flower("Purple", 42);

System.out.println(obj);

}

}

Set TRACE to true to see the constructor messages.

paulcwa at 2007-7-8 8:43:21 > top of Java-index,Java Essentials,Java Programming...