Constructor error in JAVA ?

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

}

Replies

  • Devang Sinha
    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);
    }
    }
  • Anoop Kumar
    Anoop Kumar
    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.
  • sulochana anand
    sulochana anand
    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

You are reading an archived discussion.

Related Posts

How noise can be converted into electrical energy??
Real time project ideas based on power electronics??EEE projects??
Pinterest is now available on iPad as a native app. The app lets you 'pin' your photos on the wall and share them with your contacts. The new app will...
hi CEns could anyone explain in detail of this code. #include main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q;...
please suggest me some topics for ppt. Should me mechanical related.