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
/**
* 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();
}
}
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();
}
}
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();
}
}
@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
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.