0

I'm really new to java, and I'm studying to a test I have on Thursday. I was doing some old test and the question was to create a Class that would make this Class work:

public class TestaAlbum {

    public static void main(String[] args) {

        Album album1 = new Album("Endless River", 18, 2014);
        Album album2 = new Album("The Wall", 26);
        Album album3 = new Album("Om", 11, 2015);
        //Album album4 = new Album("Heroes", 10, 1977);
        album2.setAno(1979);                // o album 2 foi originalmente publicado em 1979
        if (album2.foiEditadoNesteSeculo())
            System.out.println("O album " + album2.getTitulo() +
                    " foi editado neste seculo, em " + album2.getAno() + ".");
        else
            System.out.println("O album " + album2.getTitulo()
                    + " foi editado no seculo passado, em " + album2.getAno() + ".");
        System.out.println("O album " + album3.getTitulo() + " tem " + album3.getTemas() + " temas.");
        album3.adicionaTemasBonus(2);   // o album 3 tem mais 2 temas extra/bonus...
        album1.mostraAlbum();
        album3.mostraAlbum();

    }

    /******** Resultado esperado ao executar o programa de teste acima:

     O album The Wall foi editado no seculo passado, em 1979.
     O album Om tem 11 temas.
     Endless River (editado em 2014; tem 18 temas)
     Om (editado em 2015; tem 13 temas)

     ********/

The comment part at the end shows what the result should be (sorry it's in portuguese, but that's how the question is phrased). So I tried to create my own class, and this is really the first time I'm working with OOP, so I literally know nothing, but I don't understand why my getters and setters only affect the last object created (album 3). My Class is written as following:

public class Album {
    private static String Titulo;
    private static int temas;
    private static int ano;

    public Album(String Titulo2, int temas2, int ano2) {
        Titulo = Titulo2;
        temas = temas2;
        ano = ano2;
    }

    public Album(String Titulo2, int temas2) {
        Titulo = Titulo2;
        temas = temas2;
    }

    public String getTitulo() {
        return this.Titulo;
    }

    public int getAno() {
        return this.ano;
    }

    public int getTemas() {
        return this.temas;
    }

    public void setAno(int newAno) {
        this.ano = newAno;
    }

    public static boolean foiEditadoNesteSeculo () {
        return Album.ano > 2000;
    }

    public static void adicionaTemasBonus(int x) {
        temas += x;
    }

    public static void mostraAlbum() {
        System.out.println(Album.Titulo + " (editado em " + Album.ano + "; tem " + Album.temas + " temas)" );
    }

}

The result I'm getting is like this:

O album Om foi editado no seculo passado, em 1979.
O album Om tem 11 temas.
Om (editado em 1979; tem 13 temas)
Om (editado em 1979; tem 13 temas)
Carras
  • 27
  • 5

2 Answers2

3

You are using static members:

private static String Titulo;
private static int temas;
private static int ano;

This means that if you update any of these members for one instance of Album the, all of them will be updated.

Try this:

private String Titulo;
private int temas;
private int ano;

And change those static methods you have at the bottom of your class to this:

   public boolean foiEditadoNesteSeculo () {
        return this.ano > 2000;
    }

    public void adicionaTemasBonus(int x) {
        this.temas += x;
    }

    public void mostraAlbum() {
        System.out.println(this.Titulo + " (editado em " + this.ano + "; tem " + this.temas + " temas)" );
    }
gabriel.hayes
  • 2,267
  • 12
  • 15
  • I did it, but now the returns doesn't work. It says "Non-static field 'ano' cannot be referenced from a static context". The same happens to Titulo and temas. – Carras Jan 07 '20 at 23:20
  • Your static methods need to be non-static as well. (`foiEditadoNesteSeculo`, `adicionaTemasBonus` and `mostraAlbum`) – gabriel.hayes Jan 07 '20 at 23:22
  • The return `Album.ano` still doesn't work for some reason on the `foiEditadoNesteSeculo` and none of them `Album.Titulo`, `Album.temas` or `Album.ano` on the `mostraAlbum` method. Same error as before "Non-static field '(all)' cannot be referenced from a static context" – Carras Jan 07 '20 at 23:33
  • @Carras I've edited my answer to deal with this; the problem is when you use a static member, you are essentially saying "This belongs to the class" so `private static String Titulo` has to be accessed like this: `Album.Titulo` however, non-static members belong to the *instance* instead of the class, so you need to create an instance of `Album` first with `var album = new Album();` then you'd go `album.Titulo` – gabriel.hayes Jan 07 '20 at 23:46
  • You would use static members for something different; for example, say you wanted a count of all of the `Album` instances. You could have `private static int AlbumCount` and inside the `Album` constructor, you would go `Album.AlbumCount++`, this would increment the AlbumCount for every instance, and `Album.AlbumCount` would accurately reflect the number of albums you created with the constructor. – gabriel.hayes Jan 07 '20 at 23:47
  • However, `Titulo`, `ano` and `temas` would be different for each instance of `Album`, so they are non-static (every album has its own `Titulo` but there would be only -one- `AlbumCount`, so Titulo is non-static but AlbumCount would be static), I hope that makes sense and helps you understand static members. – gabriel.hayes Jan 07 '20 at 23:48
  • my class doesn't accept the ```var album = new Album();``` thing... I still am not able to call the methods I've created, but I understood that it isn't supposed to be a static there. I just can't understand how to work without the static. – Carras Jan 08 '20 at 00:36
  • Oh, now I got it. I had a problem with the "this" thing. Thank you!! – Carras Jan 08 '20 at 01:28
0

You need to remove the static modifier.

public class Album {
private String Titulo;
private int temas;
private int ano;

public Album(String Titulo2, int temas2, int ano2) {
    this.Titulo = Titulo2;
    this.temas = temas2;
    this.ano = ano2;
}
Maxdola
  • 1,562
  • 2
  • 10
  • 29