Advertisement
fabioceep

JAVA: Metodos estaticos

Feb 18th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. /*
  2. Testando Metodos estáticos em java.
  3. Ideia principal criar um Id.
  4. Funcionou perfeitamente
  5. */
  6.  
  7. /*
  8.  * To change this license header, choose License Headers in Project Properties.
  9.  * To change this template file, choose Tools | Templates
  10.  * and open the template in the editor.
  11.  */
  12. package estatico;
  13.  
  14. /**
  15.  *
  16.  * @author fabio
  17.  */
  18. public class Estatico {
  19.  
  20.     /**
  21.      * @param args the command line arguments
  22.      */
  23.     public static void main(String[] args) {
  24.        animais.setId(5);
  25.        System.out.println(animais.getId());
  26.        animais cao = new animais(1,"toto");
  27.        System.out.println(animais.getId());
  28.        animais gato = new animais(2,"mial");
  29.        
  30.        System.out.println(cao.getId());
  31.     }
  32.    
  33. }
  34.  
  35. ---------------------------------------------ARQUIVO animais.java-----------------------------------------------------------------
  36.  
  37. /*
  38.  * To change this license header, choose License Headers in Project Properties.
  39.  * To change this template file, choose Tools | Templates
  40.  * and open the template in the editor.
  41.  */
  42. package estatico;
  43.  
  44. /**
  45.  *
  46.  * @author fabio
  47.  */
  48. public class animais {
  49.     private static int id;
  50.     private int categoria;
  51.     private String nome;
  52.  
  53.     public animais(int categoria, String nome) {
  54.         animais.id++;
  55.         this.categoria = categoria;
  56.         this.nome = nome;
  57.        
  58.     }
  59.  
  60.     public static void setId(int id) {
  61.         animais.id = id;
  62.     }
  63.  
  64.     public static int getId() {
  65.         return id;
  66.     }
  67.    
  68.    
  69.    
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement