Results 1 to 6 of 6

Thread: why i am getting an error while calling a constructor from another constructor

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Answers
    11

    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


    friends can any one explain while i am getting this error


  2. #2
    Junior Member
    Join Date
    Jul 2007
    Answers
    1

    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.


  3. #3
    Junior Member
    Join Date
    Jul 2007
    Answers
    2

    Smile 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);
    }
    }


  4. #4
    Junior Member
    Join Date
    Oct 2006
    Answers
    2

    Smile Re: why i am getting an error while calling a constructor from another constructor

    Quote Originally Posted by raj_java_j2ee View Post
    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


    friends can any one explain while i am getting this error
    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


  5. #5
    Junior Member
    Join Date
    Oct 2006
    Answers
    2

    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


  6. #6
    Junior Member
    Join Date
    Dec 2010
    Answers
    1

    Re: why i am getting an error while calling a constructor from another constructor

    [QUOTE]
    Quote Originally Posted by raj_java_j2ee View Post
    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
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact