duke_sastry
duke_sastry

Reputation: 43

Java Variable Initialization

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?

Upvotes: 1

Views: 7466

Answers (7)

jonny2k9
jonny2k9

Reputation: 166

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.

Upvotes: 4

E_net4
E_net4

Reputation: 29972

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

Upvotes: 3

dan
dan

Reputation: 13262

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

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347184

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...

Upvotes: 2

Aubin
Aubin

Reputation: 14853

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

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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

Upvotes: 1

MrSil
MrSil

Reputation: 618

You can't define var like so.

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

Upvotes: 0

Related Questions