-1
Hello, I have a Student class with the attributes name and enrollment. The user can enter the name by setNome(), but I would like to know how to make the registration is generated automatically (1, 2 , 3, ...)
-1
Hello, I have a Student class with the attributes name and enrollment. The user can enter the name by setNome(), but I would like to know how to make the registration is generated automatically (1, 2 , 3, ...)
1
Creates a class that will hold the value static
and that will always return you the value with increment, example:
public class GeradorMatricula {
private static int ID = 1;
public static int getProximaMatricula() {
return ID++;
}
}
public static void main(String[] argv) {
System.out.println(GeradorMatricula.getProximaMatricula());
System.out.println(GeradorMatricula.getProximaMatricula());
}
1
2
The ID
should be initialized with zero.
Actually not @ramaral, it would take zero only if I returned ++ID
instead of ID++
.
You’re right, I’m sorry, my distraction.
@We’re here to help you, don’t worry :)
Browser other questions tagged java
You are not signed in. Login or sign up in order to post.
You’re saving it in a database?
– Maicon Carraro
@Maiconcarraro Não
– Ana