FAQ: Static Variables and Methods - Static Methods Refresher

This community-built FAQ covers the “Static Methods Refresher” exercise from the lesson “Static Variables and Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Static Methods Refresher

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Won’t this make it possible to get 11?
if Math.random generates 0 or 1, isn’t it suppose to be like the below code?

int randomNum = (int)(Math.random() * 9) + 1;

If we look at the documentation for the method, we see that `Math.random()` produces a double number in the range `[0.0, 1.0)`. This means that 0.0 is included in the range but 1.0 is not included in the range.
So,
`Math.random()` will give you `[0.0, 1.0)`
`Math.random() * 10` will give you `[0.0, 10.0)`
`(int)(Math.random() * 10)` will give you `[0, 10)`
`(int)(Math.random() * 10) + 1` will give you `[1, 11)` i.e. an integer which can be any of `1, 2, 3, 4, 5, 6, 7, 8, 9, 10`

1 Like

double randomNumber = Math.random();
// Stores a random decimal between 0 and 1 in randomNumber

double number = String.valueOf(“2.5”);
// Transforms the String “2.5” into a double

In the above example shows that we can change or transform String value into a double by using that method but when i try the same it throws me an error stating…

ExerciseOne.java:13: error: incompatible types: String cannot be converted to double
double number = String.valueOf(“1.7”);
^
1 error

Idk why…Can anyone explain why it occurs…

2 Likes

Very good catch. After trying to replicate why you are getting the error, I looked at the documentation. Turns out Codecademy made a mistake and got things mixed up.

If we want to transform the String “2.5” into a double, then we should use:
`double number = Double.valueOf("2.5");`

If we want to transform the double 2.5 into a String, we should use:
`String number = String.valueOf(2.5);`

So, Codecademy got things the other way around. I have submitted a bug report for the content. Hopefully, they will fix it. You may also want to submit a bug report via the “Get Unstuck” dropdown for the exercise.

2 Likes

Oh…really thanks for the reply and examples that you provided was spot on. Because of the example i just understand what the valueOf would do when we code but i have one more doubt in the same exercise

String myNewString = Integer.toString(1);

As per my understanding about the above method could change integer value into string right(Idk but i just look the code and understand that is how it work) but i want to know the real purpose of using the above method…and Thank You…

1 Like

If we wish to convert an integer to a string, we aren’t limited to one option only to accomplish the task.

One way is as shown in the exercise making use of the toString method of the Integer class.
`String myNewString = Integer.toString(1);`

Another way of doing the same would be to use the valueOf method of the String class.
`String myNewString = String.valueOf(1);`
As mentioned in a remark in the documentation,

The representation is exactly the one returned by the `Integer.toString` method of one argument.

So, you have a choice on how you want to do the conversion. (Edit: I don’t know which of the two approaches would be preferable/better).
One method is from the String class, the other is from the Integer class. So, it shouldn’t be surprising that some methods from different classes may offer us alternative ways of accomplishing the same thing.

1 Like

I am also thinking the same. Since both do the same job(conversion). That why am ask is there anything special in it or we get a different output when we use. Now your reply saved me and i understand we have optional to do so. Thank for the conformation…
Thank you very much…

1 Like