0

I'm using code from Making a simple high-score system - Codecall, but I received an error while using it. The line causing the issues is inside the HighScoreManager() class.

This is the faulty code:

public String getHighscoreString() {
    String highscoreString = "";
    Static int max = 10; // this line gives an error

    ArrayList<Score> scores;
    scores = getScores();

    int i = 0;
    int x = scores.size();
    if (x > max) {
        x = max;
    }
    while (i < x) {
        highscoreString += (i + 1) + ".\t" + scores.get(i).getNaam() + "\t\t" + scores.get(i).getScore() + "\n";
        i++;
    }
    return highscoreString;
}

The line Static int max = 10; throws

not a statement

Using a lower case "s" (static) throws

illegal start of expression

If I delete the Static it works. I don't know weather this will have a big impact on the code or not. Using a lowercase "s" doesn't work either, and the uppercase Static is what was from the website that had the code, so I don't know why they wrote it with an uppercase S.

blkpingu
  • 1,556
  • 1
  • 18
  • 41
on1222
  • 29
  • 5
  • 2
    For one, you can't use static inside a method. What do you think making this static would do for you? – Andy Turner May 09 '18 at 18:46
  • 1
    For another, `static` is a keyword, `Static` is not. – Andy Turner May 09 '18 at 18:47
  • @AndyTurner I didn't write the code, so I don't really know what the static is supposed to be doing. and changing it to a lower case doesn't work, but deleting the static completely works, but am I messing up the code if i do that? – on1222 May 09 '18 at 18:54
  • 2
    I would try to contact the author of that tutorial, to find out what they intended. This code is clearly wrong. Possibly that line was supposed to be outside the method (so move it up two lines, and make it `static` with a small `s`). But even with this fixed, I don't think this is a great tutorial. There are lots of things in the code that aren't ideal. – Dawood ibn Kareem May 09 '18 at 19:04
  • @DawoodibnKareem Okay, thank you so much for your help and time! – on1222 May 09 '18 at 19:14
  • 1
    Related / maybe duplicate: [How do I create a static local variable in Java?](https://stackoverflow.com/questions/2079830/how-do-i-create-a-static-local-variable-in-java) [Why can't we declare static variables inside a function body even when the function is static?](https://stackoverflow.com/questions/24269308/why-cant-we-declare-static-variables-inside-a-function-body-even-when-the-functi) [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – Bernhard Barker May 11 '18 at 06:56

2 Answers2

6

There is one general mistake and one illegal field declaration in your code:

  1. First up, generally: It's alway static, instead of Static. static is supposed to be lowercase. Java does not recognise it as a keyword otherwise. You can read about case sensitivity in java here.

  2. The illegal field declaration: The code compiled and executed perfectly after I deleted the static keyword from the int max = 10; in your HighscoreManager.classs getHighscoreString() method.

Besides HighscoreManager.class, I used the Main.class and Score.class and ScoreComparator.class from the website you referenced , unchanged.

Why did this happen?

You can't declare a field static in a method. It's not permitted by default.

You can read this post on the topic.

The output is:

1.  Marge       300
2.  Lisa        270
3.  Bart        240
4.  Maggie      220
5.  Homer       100
blkpingu
  • 1,556
  • 1
  • 18
  • 41
  • 2
    While true, it's not the core problem. – lexicore May 09 '18 at 18:51
  • while true, it cures the error OP is referencing, but yes, it isn't :) – blkpingu May 09 '18 at 18:55
  • @BlkPengu It still doesn't work with a lower case s. I've already tried that, but it gives me "illegal start of expression". i deleted the static and just left the int max = 10; and that works, but I don't know if that will affect what the author of the code wanted when they wrote it with "Static" – on1222 May 09 '18 at 18:58
  • @on1222 can you upload Score.class? You made me feel bad about my answer. I'll try to debug your code – blkpingu May 09 '18 at 19:02
  • @BlkPengu I used the code from this website: http://forum.codecall.net/topic/50071-making-a-simple-high-score-system/ & thank you for trying to help!! the HighscoreManager is giving me the error even though I directly copied what they posted – on1222 May 09 '18 at 19:03
2

not a statement because Static is nothing in java but it is static.

Also you cannot generate a static variable at every call of a function but it is declared in class directly. This is because it doesn't instantiate with object of that class but originally attached with class and accessible by one dot notation.(depends whether it is private or not).

Vicrobot
  • 3,795
  • 1
  • 17
  • 31