1

Here's a piece of code I wrote.

public class cube { 
   private int length;
   private int breadth;
   private int height;
   private int volume;
   private int density;
   private int weight;
   public cube(int l,int b,int h, int d)  {
      length=l;
      breadth=b;
      height=h;
      density=d;
   }

   public void volmeShow(){
      volume = length * breadth * height;
      System.out.println("The Volume of the cube is "+this.volume);
   }
}

So if I implement the above cube class like this,

public class cubeApp {
    public static void main(String[] args){
       cube mycube = new cube(5,6,9,2);
       mycube.volumeShow();
    }
}

I get an output that tells me the volume is 270.

But I get an output that says the volume is 0. On the other hand, if I define the volume variable like this:

public class cube { 
    private int length;
    private int breadth;
    private int height;
    private int volume=length*breadth*height;
    private int density;
    private int weight;
    public cube(int l,int b,int h, int d)  {
        length=l;
        breadth=b;
        height=h;
        density=d;
    }

    public void volmeShow(){
        System.out.println("The Volume of the cube is " + this.volume);
    }
}

Why this is happening?

7 Answers 7

4

Because length, breadth and height have not been assigned any values when that statement gets executed. you are better off performing that calculation in the constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, but I was just wondering why don't the values of these variable change when I implement the class with the constructor? I mean once I implemented the constructor creating a new 'cube' class with values that I have supplied, why is not this new class created with default variable values from the values I have supplied, why are the values still from the initialization of the class?
You are initializing the variables correctly, except for volume. So length, breadth, height and density will all be calculated properly. But volume won't be because the calculation volume=length*breadth*height; is executed before those variables are initialized, hence why volume is zero.
3

The only problem is that your volume variable is only defined before the construction of the object takes place. The remaining attributes are set to 0 by default, thus why the resulting volume was 0. You can modify your constructor to:

public cube(int l,int b,int h, int d)  {
    length=l;
    breadth=b;
    height=h;
    density=d;
    volume=length*breadth*height;
}

This way the volume will be properly computed. Side note, you should use Cube as the class name, see Code Conventions for the Java

2 Comments

+1 sorry, I edited by mistake your message, and was approved too.
@dan Don't bother, that was myself. Stating the actual solution is always a good idea in an answer. ;)
2

When a class is initialized, all member fields are initialized to default values (for int (and numbers in general) that is 0).

Basically

private int length;
private int breadth;
private int height;
private int density;
private int weight;
private int volume=length*breadth*height;

Evaluates to

private int length = 0;
private int breadth = 0;
private int height = 0;
private int density = 0;
private int weight = 0;
private int volume=length*breadth*height;

Which evaluates to

private int volume=0*0*0;

You then no longer update the volume value.

You would actually be better doing...

public cube(int l,int b,int h, int d)  {
    length=l;
    breadth=b;
    height=h;
    density=d;
    volume=length*breadth*height;
}

...In fact, you could do away with the length, breadth, height and density values altogether (from you example) as they don't add anything to you class...

Comments

2

Since private int volume=length*breadth*height; is executed when the class is initialized, and all the involved fields are 0.

You can modify your constructor to:

public cube(int l,int b,int h, int d)  {
    length=l;
    breadth=b;
    height=h;
    density=d;
    volume=length*breadth*height;
}

This way the volume will be properly computed. Side note, you should use Cube as the class name, see Code Conventions for the Java

Comments

1

You need to calculate and assign volume after values are set in other variables -

public cube(int l,int b,int h, int d)  { 
    length=l; 
    breadth=b; 
    height=h; 
    density=d; 
    volume=length*breadth*height;
}

But, volume looks like it is redundant in your class. You can remove it, your method can always calculate it on the fly -

public void volmeShow(){     
     int volume=length*breadth*height; //local, not needed at class level
     System.out.println("The Volume of the cube is " + volume);

Comments

0

You can't define var like so.

When you define it outside method - this will be calculated when class is created.

Comments

0

The attributes are initialized by constructor in order they are declared, before the body of constructor but after a call to super().

At the time of initialization of volume, in your case, all the member are set to zero, which is the default value for int.

You have to write :

public cube(int l,int b,int h, int d)  {
    length  = l;
    breadth = b;
    height  = h;
    density = d;
    volume  = length*breadth*height;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.