Warming up the brain is useful, and this is done best with puzzles. But because we're programmers, we put puzzles right into the code. Here you go: for this task, you need to place the minimum number static modifiers necessary to make the code start working and the program complete successfully.
Minimum number of statics
- 5
Locked
Comments (21)
- Popular
- New
- Old
You must be signed in to leave a comment
Hoist
6 December 2021, 04:25
Please have a look. This will __NOT__ compile ????? No clue. Any thoughts here with additional explanations ?
0
Javokhir Akromjonov
21 September 2021, 22:44
what does it mean?
0
Tianfeng
29 September 2021, 13:19
It means creating an object of Class Solution but with no variable reference it.
0
Javokhir Akromjonov
29 September 2021, 21:22
thanks
0
Brandon Leirer
16 June 2020, 18:46
Add to the list of tasks that we weren't adequately trained for.
+5
hemant
30 March 2020, 06:46
why method 4 isn't static
public void method(){...
0
Michaela Obertova
10 October 2022, 12:13
Static methods can only be called from static context, so what you need to do in this task is to find the places where a static method is called and find the places where static needs to be used based on that. The task is to use the minimum of static methods so as long as you have a method that doesn't need to be static in order for the code to compile, you don't need to make it static.
0
carter
24 September 2019, 02:02
why do we make the int static?
+2
Nickolas Johnson
17 March 2020, 12:32
if step isn't a static variable it will never increment past 1. If it never increments past 1 it just runs forever. I assume the compiler built into codegym has a way of preventing this so someone doesn't accidentally use up a ton of their website server's resources but if you run this on your computer without the static modifier for step it will just run forever.
+3
Corina Bodea
18 March 2020, 16:46
Good question. :)
Hope this will be useful also for further codegym fellows in need.
We know that a static variable is shared across all instances of a class. No matter how many instances of the class we create, the static variable remains the same.
Let's take an example:
What do you think will be the count value?
Well... count is actually 3. Why? Because as I said, static variable is shared across all instances.
First our count becomes 2, then 5, and finally 3 ( even if we created 2 instances of the Panda class)
So, in our task, method2 looks like this:
It creates a new instance of the class, meaning that a static variable will NOT be affected by it. step will remain 1 after first increment. BUT, an instance variable will be influenced, step becomes 0 again so finally will get into an infinite loop.
+6
luna Zheng
16 September 2019, 02:49
"Static method can't address non-static variables"
it's the key to this task. The main function is static.So the function called directly by main function must be also static.
if a static method calls a non-static method, a object must be created.Then the method in the object can be called.
for the non-static method, it can call another non-static method in the same class directly.
That's my opinion.Hope it's hopeful.
+21
fifi deng
30 September 2019, 10:06
nice solution
0
Daniel
11 January 2020, 05:30
Thank you. This was better than providing the solution, it actually made me understand "the tricky part".
+1
Ahmad Java Developer
9 April 2021, 12:01
Your explanation really helped me understand. Thank you!
0
Vrh Backend Developer
25 June 2019, 12:41
This one is really hard to think through for me...
Why is method3 non-static? If it calls method4 without creating an object. I suppose this has to do with method2 creating an object of method3 but I would like to get further explanation.
+1
Ahmad Java Developer
9 April 2021, 12:10
It's because method2 is creating an object to call method3. So because of that we can't make method3 static. Otherwise, then we have to change the way method3 is calling method3 and that goes against the rules for this specific task ("Don't change the method implementations...").
0
Andrew
29 January 2019, 07:34
What does this line mean?
new Solution().method3();
Thanks,
A
+1
Johan
21 March 2019, 11:25
this implement the Solution class in use the method 3
0
Muhammad Vahhaaj
15 June 2019, 07:42
new Solution() , creates a new object therefore it is substituted by the reference of that object.
Next the method is called by using that reference (this.method3());
Where this contains the reference of that object
+1
Arko Sarkar
30 August 2018, 06:53
I solved this but didn't understand few things:
1. Solution().method3() means?
2. Why step has to be static variable? (Gave too long errors if it isn't!)
+1
Justin Smith
10 November 2019, 20:01
1. It means that it is creating a new Solution object and calling on the method inside of the solution class called method3.
2. This has to do with the rule that a static method cannot address non-static variables.
+8