Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Testando Metodos estáticos em java.
- Ideia principal criar um Id.
- Funcionou perfeitamente
- */
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package estatico;
- /**
- *
- * @author fabio
- */
- public class Estatico {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- animais.setId(5);
- System.out.println(animais.getId());
- animais cao = new animais(1,"toto");
- System.out.println(animais.getId());
- animais gato = new animais(2,"mial");
- System.out.println(cao.getId());
- }
- }
- ---------------------------------------------ARQUIVO animais.java-----------------------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package estatico;
- /**
- *
- * @author fabio
- */
- public class animais {
- private static int id;
- private int categoria;
- private String nome;
- public animais(int categoria, String nome) {
- animais.id++;
- this.categoria = categoria;
- this.nome = nome;
- }
- public static void setId(int id) {
- animais.id = id;
- }
- public static int getId() {
- return id;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement