Constructor error in JAVA ?

Devang Sinha
@devang-sinha-IaKU89 โ€ข Oct 26, 2024

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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Devang Sinha

    @devang-sinha-IaKU89 • Aug 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);
    }
    }

  • Anoop Kumar

    @anoop-kumar-GDGRCn • Aug 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.

  • sulochana anand

    @sulochana-anand-OrGmKr • Aug 15, 2012

    Devang SinhaHere 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