Non-static variable this cannot be referenced from a static context

cs_organik Messages postés 1 Date d'inscription lundi 10 janvier 2005 Statut Membre Dernière intervention 17 janvier 2005 - 17 janv. 2005 à 11:52
JBAware Messages postés 174 Date d'inscription lundi 23 septembre 2002 Statut Membre Dernière intervention 6 avril 2011 - 19 janv. 2005 à 13:35
Bonjour..j'ai un souci à la compilation d'un programme...

j'ai le message d'erreur :

"non-static variable this cannot be referenced from a static context"



voici le fichier principal contenant la methode main :



import java.util.*;



public class EnsPt1

{

public static void main()

{

Point p1 = new Point(1,3);

Point p2 = new Point(2,2);

Point p3 = new Point(4,5);

Point p4 = new Point(1,8);

Point p[] = {p1,p2,p1,p3,p4,p3};

HashSet ens = new HashSet();

for (int i = 0;i<p.length; i++)

{

System.out.print("le point");

p[i].affiche();

boolean ajoute = ens.add(p[i]);

if (ajoute) System.out.print(" a été ajouté");

else System.out.println(" est déja présent");

System.out.println("ensemble = ");

affiche(ens);

}

}



public static void affiche(HashSet ens)

{

Iterator iter = ens.iterator();

while (iter.hasNext())

{

Point p = (Point)iter.next();

p.affiche();

}

System.out.println();

}



class Point

{

Point(int x ,int y)

{

this.x = x;

this.y = y;

}



public int hashCode()

{

return x+y;

}



public boolean equals (Object pp)

{

Point p = (Point) pp;


return ((this.x p.x) & (this.x p.y));

}



public void affiche()

{


System.out.print("[" + x + " " + y + "]");

}

private int x, y;

}

}



et le fichier contenant la classe point :





public class Point

{

public Point(int abs, int ord) //constructeur

{

x=abs;

y=ord;

}



public void deplace(int dx, int dy)

{

x += dx;

y += dy;

}



public void affiche()

{

System.out.println("Je suis un point de coordonnées "+x+" "+y);

}



private int x;

private int y;

}



merci d'avance pour votre aide...
A voir également:

1 réponse

JBAware Messages postés 174 Date d'inscription lundi 23 septembre 2002 Statut Membre Dernière intervention 6 avril 2011 1
19 janv. 2005 à 13:35
Salut,

J'ai une question :

Ta classe Point est définie deux fois et qui plus est une fois en tant que innerclass de EnsPt1

pourquoi?

ensuite ton probleme se trouve exactement dans la innerclass Point de EnsPt1 et plus précisément à chaque fois que tu fais un appel à ses attributs x et y en employant "this" devant:
this.y
this.x

Bon courage

JB@WAre
0
Rejoignez-nous