Citation :
 
 
 The static keyword
   Le mot-clef static      
 Ordinarily, when you create a class you
 are describing how objects of that class look and how they will behave. You
 don?t actually get anything until you create an object of that class with
 new, and at that point data storage is created and methods become
 available.
   Normalement, quand on crée une classe, on décrit ce à quoi ressemblent les objets de cette classe et comment ils se comportent. Rien n'existe réellement avant de créer un objet de cette classe avec new ; à ce moment la zone de données est créée et les méthodes deviennent disponibles.  
    
 But there are two situations in which
 this approach is not sufficient. One is if you want to have only one piece of
 storage for a particular piece of data, regardless of how many objects are
 created, or even if no objects are created. The other is if you need a method
 that isn?t associated with any particular object of this class. That is,
 you need a method that you can call even if no objects are created. You can
 achieve both of these effects with the static keyword. When you say
 something is static, it means that data or method is not tied to any
 particular object instance of that class. So even if you?ve never created
 an object of that class you can call a static method or access a piece of
 static data. With ordinary, non-static data and methods you must
 create an object and use that object to access the data or method, since
 non-static data and methods must know the particular object they are
 working with. Of course, since static methods don?t need any
 objects to be created before they are used, they cannot directly access
 non-static members or methods by simply calling those other members
 without referring to a named object (since non-static members and methods
 must be tied to a particular object).
   Mais il y a deux situation pour lesquelles cette approche n'est pas suffisante. L'une, si on veut avoir une zone de stockage pour des données spécifiques, sans tenir compte du nombre d'objets créés, ou même si aucun objet n'a été créé. L'autre, si on a besoin d'une méthode qui n'est associée à aucun objet particulier de la classe. C'est à dire si on a besoin d'une méthode qui puisse être appelée même si aucun objet n'a été créé. On peut obtenir ces deux effets avec le mot-clef static. Dire que quelque chose est static signifie que la donnée ou la méthode n'est pas spécifiquement rattachée à un objet instance de cette classe. Donc, même si aucun objet de cette classe n'a jamais été créé il est possible d'appeler une méthode static ou d'accéder à une donnée static. Avec des données et des méthodes non static ordinaires il faut connaître l'objet spécifique avec lequel elles fonctionnent. Bien entendu, étant donné que les méthodes static n'ont pas besoin qu'un objet soit créé avant d'être utilisées, elles ne peuvent pas accéder directement à des membres ou des méthodes non static en appelant ces autres membres sans faire référence à un objet nommé (puisque les membres et méthodes non static doivent être rattachés à un objet spécifique).  
    
 Some object-oriented languages use the
 terms class data and class methods, meaning that the data and
 methods exist only for the class as a whole, and not for any particular objects
 of the class. Sometimes the Java literature uses these terms
 too.
   Certains langages orientés objet emploient les expressions données de classe et méthodes de classe, ce qui signifie que les données et les méthodes n'existent que pour la classe en tant que tout et pas pour des objets particuliers de la classe. Parfois la littérature Java utilise aussi ces expressions.  
    
 To make a data member or method
 static, you simply place the keyword before the definition. For example,
 the following produces a static data member and initializes
 it:
     Pour rendre statique une méthode ou une donnée membre il suffit de mettre le mot-clef static avant la définition. Par exemple, le code suivant crée une donnée membre static et l'initialise :  
    
 class StaticTest {
     static int i = 47;
 }     class StaticTest {
     static int i = 47;
 }
  
    
 Now even if you make two StaticTest objects, there will still be only one piece of storage for StaticTest.i. Both objects will share the same i. Consider:   Maintenant, même en créant deux objet StaticTest, il n'y aura qu'une seule zone de stockage pour StaticTest.i. Tous les objets partageront le même i. Considérons :  
    
 StaticTest st1 = new StaticTest();
 StaticTest st2 = new StaticTest();     StaticTest st1 = new StaticTest();
 StaticTest st2 = new StaticTest();
  
    
 At this point, both st1.i and st2.i have the same value of 47 since they refer to the same piece of memory.   à ce point, st1.i et st2.i ont la même valeur 47 puisqu'elles font référence à la même zone mémoire.  
    
 There are two ways to refer to a static variable. As indicated above, you can name it via an object, by saying, for example, st2.i. You can also refer to it directly through its class name, something you cannot do with a non-static member. (This is the preferred way to refer to a static variable since it emphasizes that variable?s static nature.)   Il y a deux façons de faire référence à une variable static. Comme indiqué ci-dessus, il est possible de la nommer via un objet, en disant par exemple st2.i. Il est aussi possible d'y faire référence directement par le nom de la classe, ce qui ne peut pas être fait avec un membre non static (c'est le moyen de prédilection pour faire référence à une variable static puisque cela met en évidence la nature static de la variable).  
    
 StaticTest.i++;
     StaticTest.i++;
  
    
 The ++ operator increments the
 variable. At this point, both st1.i and st2.i will have the value
 48.
   L'opérateur ++ incrémente la variable. À ce point, st1.i et st2.i auront tous deux la valeur 48.  
    
 Similar logic applies to static methods.
 You can refer to a static method either through an object as you can with any
 method, or with the special additional syntax ClassName.method( ).
 You define a static method in a similar way:
     Une logique similaire s'applique aux méthodes statiques. On peut faire référence à une méthode statique soit par l'intermédiaire d'un objet, comme on peut le faire avec n'importe quelle méthode, ou avec la syntaxe spécifique supplémentaire ClassName.method( ). Une méthode statique est définie de façon similaire :  
    
 class StaticFun {
   static void incr() { StaticTest.i++; }
 }     class StaticFun {
   static void incr() { StaticTest.i++; }
 }
  
    
 You can see that the StaticFun method incr( ) increments the static data i. You can call incr( ) in the typical way, through an object:   On peut voir que la méthode incr( ) de StaticFun incrémente la donnée static i. On peut appeler incr( ) de façon classique, par le biais d'un objet :  
    
 StaticFun sf = new StaticFun();
 sf.incr();     StaticFun sf = new StaticFun();
 sf.incr();
  
    
 Or, because incr( ) is a static method, you can call it directly through its class:   Ou, parce que incr( ) est une méthode statique, il est possible de l'appeler directement par sa classe :  
    
 StaticFun.incr();
     StaticFun.incr();
  
    
 While static, when applied to a
 data member, definitely changes the way the data is created (one for each class
 vs. the non-static one for each object), when applied to a method
 it?s not so dramatic. An important use of static for methods is to
 allow you to call that method without creating an object. This is essential, as
 we will see, in defining the main( ) method that is the entry point
 for running an application.
   Alors que static, lorsqu'il est appliqué à une donnée membre, change sans aucun doute la façon dont la donnée est créée (une pour chaque classe par opposition à une pour chaque objet dans le cas des données non statiques), lorsqu'il est appliqué à une méthode, le changement est moins significatif. Un cas important d'utilisation des méthodes static est de permettre d'appeler cette méthode sans créer d'objet. C'est essentiel, comme nous le verrons, dans la définition de la méthode main( ) qui est le point d'entrée pour exécuter une application.  
    
 Like any method, a static method
 can create or use named objects of its type, so a static method is often
 used as a ?shepherd? for a flock of instances of its own
 type.
   Comme pour toute méthode, une méthode statique peut créer ou utiliser des objets nommés de son type, ainsi les méthodes statiques sont souvent utilisées comme « berger » pour un troupeau d'instances de son propre type.  
 
   |