CrazyEngineers
  • Constructor error in JAVA ?

    Updated: Oct 26, 2024
    Views: 1.5K
    class box
    {
    int height;
    int width;
    int length;
    box(int h=0, int w=0, int l=0)
    {
    height=h;
    width=w;
    length=l;
    }
    int vol()
    {
    return height*width*length;
    }
    }
    public class consdemo
    {

    public static void main(String[] args)
    {
    box b1=new box(44,55,66);
    int volume;
    volume=b1.vol();
    System.out.println(volume);
    }

    }
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Devang Sinha

    MemberAug 14, 2012

    Here is a JAVA program in which i'm using a constructor (box) with default arguments. But i am getting the following error on compilation (i am using Eclipse IDE):
    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor box(int, int, int) is undefined at constrdemo.main"

    Please help me clearing my doubt.. Thanks..

    ************************************************************************
    class box
    {
    int height;
    int width;
    int length;
    box(int h=0, int w=0, int l=0)
    {
    height=h;
    width=w;
    length=l;
    }
    void vol()
    {
    System.out.println(height*width*length);
    }
    }
    public class constrdemo
    {
    public static void main(String[] args)
    {
    box b1=new box(44,55,66);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberAug 14, 2012

    change
    box(int h=0, int w=0, int l=0)
    to
    box(int h, int w, int l);

    You are assigning value in method argument.this value is set by calling position that is:
    box b1=new box(44,55,66); here you are setting those values.
    Are you sure? This action cannot be undone.
    Cancel
  • sulochana anand

    MemberAug 15, 2012

    Devang Sinha
    Here is a JAVA program in which i'm using a constructor (box) with default arguments. But i am getting the following error on compilation (i am using Eclipse IDE):
    "Exception in thread "main" java.lang.Error: Unresolved compilation problem:
    The constructor box(int, int, int) is undefined at constrdemo.main"

    Please help me clearing my doubt.. Thanks..

    ************************************************************************
    class box
    {
    int height;
    int width;
    int length;
    box(int h=0, int w=0, int l=0)
    {
    height=h;
    width=w;
    length=l;
    }
    void vol()
    {
    System.out.println(height*width*length);
    }
    }
    public class constrdemo
    {
    public static void main(String[] args)
    {
    box b1=new box(44,55,66);
    }
    }
    do compile javac box.java and execute java constrdemo
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register