Forum Stats

  • 3,827,131 Users
  • 2,260,743 Discussions
  • 7,897,181 Comments

Discussions

page 5 of the slide for Lesson 2-3

3462211
3462211 Member Posts: 27
edited May 15, 2017 2:05PM in Java Puzzle Ball

Hi All,

On page 5 of the slide for Lesson 2-3, it stated "Individual Red Bumpers start with an additional rotation of 0."

The Java code used to demonstrated this is copy typed as below;

public class RedBumper {

private double rotation;

     public void methodA() {

          rotation = rotation + 90.0;

     }

}

How do I know an individual Red Bumpers start with an additional rotation of 0 when the value of the variable "rotation" is not initialized to 0 where the the field "rotation" is declared in the class RedBumper definition?

Would it causes any logical or compiling error if I changed the code to as following;

public class RedBumper {

private double rotation = 0;

     public void methodA() {

          rotation = rotation + 90.0;

     }

}

Any thought/opinion from anyone?

Thanks,

Carol

3462211

Answers

  • NickR2600-Oracle
    NickR2600-Oracle Member Posts: 530 Employee
    edited May 15, 2017 2:05PM

    Hi Carol,

    Excellent question and observation!  Both of those code examples work the same.  If a field which uses a number (like a double or int) isn't explicitly given an initial value, it's exactly like setting the initial value to 0.  In other words, it defaults to 0.

    Nick

    34622113462211