IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Java Discussion :

Problème de création d'objets


Sujet :

Langage Java

  1. #1
    Membre habitué
    Inscrit en
    Octobre 2005
    Messages
    259
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2005
    Messages : 259
    Points : 126
    Points
    126
    Par défaut Problème de création d'objets
    Bonjour,

    J'ai un petit problème concernant la création d'un objet.

    Voici ma classe principale (elle est censée créer 2 objets du type Polygone):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class NetNest {
     
    	private float[] coordinatesA = {5, 2, 8, 3, 7, 8, 5, 5, 7, 10};
    	private float[] coordinatesB = {4, 4, 5, 2, 8, 7, 5, 9};
     
    	private Polygone polygoneA = null;
    	private Polygone polygoneB = null;
     
    	public static void main(String[] args) {
     
    	polygoneA = new Polygone(false, coordinatesA);
    	polygoneB = new Polygone(true, coordinatesB);
     
    	}
     
    }
    Et voici maintenant la classe Polygone (placée dans le même projet sous eclipse mais dans un autre fichier):

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class Polygone {
     
    	private boolean isThePiece = false;
     
    	private float[] coordinates = null;
     
    	public Polygone(boolean isIt, float[] coord){
     
    		isThePiece = isIt;
     
    		for(int i = 0; i < coord.length; i++)
    		{
    			coordinates[i] = coord[i];			
    		}
    	}
    }
    J'ai une erreur aux lignes suivantes:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    polygoneA = new Polygone(false, coordinatesA);
    polygoneB = new Polygone(true, coordinatesB);
    Cannot make a static reference to the non-static field polygoneA
    Cannot make a static reference to the non-static field coordinatesA


    Cannot make a static reference to the non-static field polygoneB
    Cannot make a static reference to the non-static field coordinatesB


    Quelqu'un peut-il ma'ider à résoudre ce problème svp? Faut-il importer un fichier dans l'autre? Comment faire?

    Merci pour votre aide

  2. #2
    Membre habitué Avatar de BlackWood
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    167
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 167
    Points : 169
    Points
    169
    Par défaut
    La méthode main() est static. Donc indépendante de toute instance de la classe courante. Or, tu utilises dans ton main() deux attributs de ta classe courante. Ce code est donc incorrect.
    Soit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    private static Polygone polygoneA = null;
    private static Polygone polygoneB = null;
     
    public static void main(String[] args) {
    	polygoneA = new Polygone(false, coordinatesA);
    	polygoneB = new Polygone(true, coordinatesB);
    }
    soit (qui est beaucoup mieux) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private Polygone polygoneA;
    private Polygone polygoneB;
     
    public NetNest() {
    	polygoneA = new Polygone(false, coordinatesA);
    	polygoneB = new Polygone(true, coordinatesB);
    }
     
    public static void main(String[] args) {
    	new NetNest();
    }
    BlackWood
    Et comme apparemment, ça fait "class" dans une signature :
    , , , ,

  3. #3
    Membre habitué
    Inscrit en
    Octobre 2005
    Messages
    259
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Octobre 2005
    Messages : 259
    Points : 126
    Points
    126
    Par défaut
    En effet ca va un peu mieux....

    Merci pour ta réponse.

  4. #4
    Membre éprouvé
    Profil pro
    Développeur Back-End
    Inscrit en
    Avril 2003
    Messages
    782
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Back-End

    Informations forums :
    Inscription : Avril 2003
    Messages : 782
    Points : 935
    Points
    935
    Par défaut
    Citation Envoyé par gids01
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class Polygone {
     
    	private boolean isThePiece = false;
     
    	private float[] coordinates = null;
     
    	public Polygone(boolean isIt, float[] coord){
     
    		isThePiece = isIt;
     
    		for(int i = 0; i < coord.length; i++)
    		{
    			coordinates[i] = coord[i];			
    		}
    	}
    }
    et pour eviter l'erreur sur le constructeur de Polygone tu peux remplacer

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    for(int i = 0; i < coord.length; i++)
    {
    	coordinates[i] = coord[i];			
    }
    par

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 03/06/2011, 14h48
  2. [Débutant] Problème de création d'objet (ActiveX))
    Par Mat32 dans le forum VB 6 et antérieur
    Réponses: 11
    Dernier message: 02/06/2011, 15h00
  3. Problème de création d'objet
    Par Maldus dans le forum Débuter
    Réponses: 2
    Dernier message: 08/12/2008, 15h37
  4. Vbs et html problème de création d'objet
    Par Picco dans le forum VBScript
    Réponses: 0
    Dernier message: 06/10/2008, 14h48
  5. Problème de création d'objet
    Par Gouyon dans le forum Delphi
    Réponses: 2
    Dernier message: 15/05/2007, 13h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo