why i am getting an error while calling a constructor from another constructor
class another_cons
{
int x,y;
another_cons(int a,int b)
{
x=a;
y=b;
}
another_cons()
{
another_cons(1,2);
}
public static void main(String args[])
{
another_cons p=new another_cons();
System.out.println("x="+p.x+"y="+p.y);
}
}
C:\forte_jdk\j2sdk1.4.0\bin>javac another_cons.java
another_cons.java:11: cannot resolve symbol
symbol : method another_cons (int,int)
location: class another_cons
another_cons(1,2);
^
1 error
[B]friends can any one explain while i am getting this error [/B]
Re: why i am getting an error while calling a constructor from another constructor
Hi,
You have to use "this" operator to call oe constructor from another.
Re: why i am getting an error while calling a constructor from another constructor
class another_cons
{
int x,y;
another_cons(int a,int b)
{
x=a;
y=b;
}
another_cons()
{
this(1,2);
}
public static void main(String args[]){
another_cons p=new another_cons();
System.out.println("x="+p.x+"y="+p.y);
}
}
Re: why i am getting an error while calling a constructor from another constructor
[QUOTE=raj_java_j2ee;14023]class another_cons
{
int x,y;
another_cons(int a,int b)
{
x=a;
y=b;
}
another_cons()
{
another_cons(1,2);
}
public static void main(String args[])
{
another_cons p=new another_cons();
System.out.println("x="+p.x+"y="+p.y);
}
}
C:\forte_jdk\j2sdk1.4.0\bin>javac another_cons.java
another_cons.java:11: cannot resolve symbol
symbol : method another_cons (int,int)
location: class another_cons
another_cons(1,2);
^
1 error
[B]friends can any one explain while i am getting this error [/B][/QUOTE]
Hi Farheen,
You have made a small mistake in this program,the error which is coming up is because of athe name of the class and constructor used in small letters,you should have used Another_cons instead of another_cons,ur program will run smoothly if u will use Another_cons and save ur file with this name only.I have tried to run this program and it has run successfully.Ther is one more thing to be noticed thatg constructor has no return type.It should have the void keyword.The code mentioned below will definately run and help u.
class Another_cons
{
int x,y;
public void Another_cons(int a,int b)
{
x=a;
y=b;
}
Another_cons()
{
Another_cons(1,2);
}
public static void main(String args[])
{
Another_cons p=new Another_cons();
System.out.println("x="+p.x+"y="+p.y);
}
}
Thanx & Regards
Gaurav Bhasin
Re: why i am getting an error while calling a constructor from another constructor
"=="
checks for the object in the memory location,whether both the string are pointing to the same memory location or another.
whereas
"equals"
check for similarity or equality between two strings
Re: why i am getting an error while calling a constructor from another constructor
[QUOTE][QUOTE=raj_java_j2ee;14023]class another_cons
{
int x,y;
another_cons(int a,int b)
{
x=a;
y=b;
}
another_cons()
{
another_cons(1,2);
}
public static void main(String args[])
{
another_cons p=new another_cons();
System.out.println("x="+p.x+"y="+p.y);
}
}
C:\forte_jdk\j2sdk1.4.0\bin>javac another_cons.java
another_cons.java:11: cannot resolve symbol
symbol : method another_cons (int,int)
location: class another_cons
another_cons(1,2);
^
1 error
[/QUOTE]
You cannot and must not call another constructor of the same or even different class from a constructor. Reason: Call to any other constructor requires the 'this' pointer which has not been created yet since the constructor has not finished yet.