Generate sequential number in java

0

Hello, I have a Student class with the name and license plate attributes. The user can enter the name by the setNome (), but I would like to know how to make the registration automatically generated (1, 2, 3, ...)

    
asked by anonymous 07.06.2015 / 19:50

1 answer

0

Create a class that will have the value static and will always return the value with increment, eg:

Matricula.java Generator

public class GeradorMatricula {
    private static int ID = 1;

    public static int getProximaMatricula() {
        return ID++;
    }
}

Using

public static void main(String[] argv) {
    System.out.println(GeradorMatricula.getProximaMatricula());
    System.out.println(GeradorMatricula.getProximaMatricula());
}

Output

  

1

     

2

    
07.06.2015 / 21:05