Minimum number of statics

  • 5
  • Locked
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.
You can't complete this task, because you're not signed in.
Comments (21)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Hoist
Level 22 , San Diego, United States
6 December 2021, 04:25
Please have a look. This will __NOT__ compile ????? No clue. Any thoughts here with additional explanations ?
package com.codegym.task.task06.task0616;
/*
Minimum number of statics
*/
// Instance variables declared as static are, essentially, global variables.
// When objects of its class are declared, no copy of a static variable is made.
// Instead, all instances of the class share the same static variable.
// We know that a static variable is shared across all instances of a class.

public class Solution {

    public static int step;

    public static void main(String[] args) {
        method1();
    }

    public static void method1() {
        method2();
    }

    public static void method2() {
        new Solution().method3();
    }

    public  void method3() {
        method4();
    }

    public  void method4() {
        step++;
        for (StackTraceElement element : Thread.currentThread().getStackTrace())
            System.out.println(element);
        if (step > 1)
            return;
        main(null);
    }
}
Javokhir Akromjonov
Level 16 , Russia, Russian Federation
21 September 2021, 22:44
what does it mean?
new Solution().method3();
Tianfeng
Level 15 , Nanjing, China
29 September 2021, 13:19
It means creating an object of Class Solution but with no variable reference it.
Javokhir Akromjonov
Level 16 , Russia, Russian Federation
29 September 2021, 21:22
thanks
Brandon Leirer
Level 7 , Keller, United States
16 June 2020, 18:46
Add to the list of tasks that we weren't adequately trained for.
hemant
Level 8 , prayagraj, India
30 March 2020, 06:46
why method 4 isn't static public void method(){...
Michaela Obertova
Level 29 , Prievidza
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.
carter
Level 8 , Middletown, United States
24 September 2019, 02:02
why do we make the int static?
Nickolas Johnson
Level 8 , St. Louis, United States of America
17 March 2020, 12:32
if (step > 1)
    return;
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.
Corina Bodea
Level 14 , Cluj Napoca, Romania
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:
public class Panda {
public static int count = 0;
public static void main(String[] args) {
Panda.count = 2;       // we initialise static count by 2
Panda panda1 = new Panda();   // we create an instance of the Panda class
Panda panda2 = new Panda();   // another instance of the Panda class
panda1.count = 5;
panda2.count = 3;
}
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:
public void method2() {
        new Solution().method3();
    }
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.
luna Zheng
Level 18 , Ningbo, China
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.
fifi deng
Level 22 , Paris, France
30 September 2019, 10:06
nice solution
Daniel
Level 15 , Colnbrook, United Kingdom
11 January 2020, 05:30
Thank you. This was better than providing the solution, it actually made me understand "the tricky part".
Ahmad Java Developer
9 April 2021, 12:01
Your explanation really helped me understand. Thank you!
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.
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...").
Andrew
Level 16 , Toronto, Canada
29 January 2019, 07:34
What does this line mean? new Solution().method3(); Thanks, A
Johan
Level 22 , La Paz, Bolivia, Plurinational State of
21 March 2019, 11:25
this implement the Solution class in use the method 3
Muhammad Vahhaaj
Level 19 , Rawalpindi, Pakistan
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
Arko Sarkar
Level 8 , Mumbai, India
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!)
Justin Smith
Level 8 , United States
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.