Anton Loubman
Anton Loubman

Reputation: 71

Why doesn't the following code make a new value for the variable?

class Cube{
    int side;
    int area = side * side;
}

public class Chronos{
    public static void main(String[]args) {
        Cube a = new Cube();
        a.side = 2;
        System.out.println(a.area);
    }
}

My question is why does it output "0" instead of multiplying the given variable a.side=2 by itself and giving a.area=4. My thought process was that it would see the command to print a.area, check back with Cube and see it's supposed to multiply a.side, which had been determined earlier, by itself.

Upvotes: 2

Views: 119

Answers (5)

Aashishkebab
Aashishkebab

Reputation: 347

This is more of a problem with the fundamental understanding of how classes are initialized.

The area = side * side statement is executed when the class is instantiated, or in other words when you do Cube a = new Cube();.

After that, area is never modified again. This is why you should never initialize class member variables to a non-constant value, as you could easily lose track of what it is.

You should make a new method to compute the area, such as:

class Cube{
    int side;

    public int getArea(){
        return side * side;
    }
}

Upvotes: 0

Khadhar Koneti
Khadhar Koneti

Reputation: 189

When you are creating a object it will take default values. You are assigning a value to variable that won't refresh. So better to maintain setters and getters and then perform your operations accordingly

Upvotes: 0

Eran
Eran

Reputation: 393831

When you create a Cube instance, first the instance variables are initialized and then the constructor body is executed.

int side;

is assigned a default value of 0

int area=side*side;

is assigned 0, since 0*0 is 0

Only later, after the Cube instance is initialized, your a.side=2 statement sets a.side to 2, but that doesn't affect the already computed a.area, which is not re-calculated.

If you allow the side property to be modified after the instance is created, you must modify area whenever side is updated. Or you can eliminate the area property and calculate it on demand.

That's what setters and getters are for:

Either modify area when side is modified:

public void setSide (int side) {
    this.side = side;
    this.area = side * side;
}

or remove the area property, and instead use:

public int computeArea () {
    return side * side;
}

Your corresponding main would be:

public static void main(String[]args) {
    Cube a = new Cube();
    a.setSide(2);
    System.out.println(a.getArea()); // if setSide also modifies the area property
}

or

public static void main(String[]args) {
    Cube a = new Cube();
    a.setSide(2);
    System.out.println(a.computeArea()); // if you remove the area property
}

Upvotes: 0

Zeta Reticuli
Zeta Reticuli

Reputation: 329

because

int area=side*side 

happens when you initialize a cube object, so it is set to, as 0*0 = 0;

To change this you have to make a method i.e. setSide or getArea, and do the calculation there. i.e. (no need to use the area variable at all)

public int getArea(){
   return side*side;
}

Upvotes: 5

Suresh Atta
Suresh Atta

Reputation: 121998

Cube a=new Cube();

When you do that, The variables are initialised with default values. And later you are just setting the variable size. That doesn't mean that the whole class magiacally refreshes.

While initialising both variables initialized with default values (0). Hence you seeing 0's.

You need to set area too. Or ideally, write a method to give area. And there you can return size*size

class Cube{
    int side;
    public int getArea(){
      return side * side;
    }
}

And then in main

public class Chronos{
    public static void main(String[]args) {
        Cube a = new Cube();
        a.side = 2;
        System.out.println(a.getArea());
    }
}

This is how you get latest values.

Upvotes: 1

Related Questions