I have a class Person, which have unique id of person (type int) for each Person object. I also have a static method isAlreadyStored(String name), which should check if a person with this name was already created. I can't solve this one by creating a list for all created Person objects, because I get error "non-static variable cannot be referenced from a static context", but I don't have any other idea how to iterate over all Person objects to find one with a given name. How do I approach this?
Asked
Active
Viewed 2,333 times
-7
-
3It's impossible to answer a question phrased like this. Share some code and the error you're getting – Mureinik Nov 25 '16 at 16:04
-
Please post your full class code. Maybe you want to invoke a non-static method from the main method (which is static)? – Shem Nov 25 '16 at 16:05
-
@Mureinik For an unanswerable question (btw: I agree), we have an amazing amount of answers. – Seelenvirtuose Nov 25 '16 at 16:19
-
2@Seelenvirtuose Anwers with questionable quality. – Tom Nov 25 '16 at 16:20
-
1@Tom Yes, I see. And I find it always difficult to deal with them. Should we downvote them? Should we leave them as they are? Why do so many people answer such questions that really do no good for SO? – Seelenvirtuose Nov 25 '16 at 16:22
-
1We should down-vote and delete the question -- it's of poor quality and it is doubtful that it will ever help anyone in the future. – Hovercraft Full Of Eels Nov 25 '16 at 16:24
-
If you need to be an expert to ask a question, probably there won't be much. – Andres Nov 25 '16 at 16:25
-
1@Andres SO started as a high-quality Q&A. Nowadays, so many questions are not better than "I have a problem. Please help me." Does such a question (and it's answer) help the community? I doubt. _SO is for the community!_ Additionally, you do not need to be an expert to ask a question. But you should be polite enough and phrase your question well enough. Not more and not less. – Seelenvirtuose Nov 25 '16 at 16:27
-
Well, that IS an ellitist comment – Andres Nov 25 '16 at 16:30
-
1Well, I got my answer, so it is certainly not impossible to answer my question. – Dark Archon Nov 25 '16 at 16:55
-
1This isn't a help site but a question and answer site, one key difference being that the quality of the question matters, probably more than the quality of the answers. – Hovercraft Full Of Eels Nov 25 '16 at 17:03
2 Answers
1
Apparently for solving your problem you need a list of all created instances for Person class. You should store that in a static variable, and then search on it. Something like this:
final static allPeople List<Person> = new ArrayList<Person>();
Then you could search on that list with something like this:
...
if (allPeople.contains(aPerson)){
...

Andres
- 10,561
- 4
- 45
- 63
0
The error non-static variable cannot be referenced from a static context means that you are trying to access a variable defined without the keyword static
from a method defined with the keyword static
.
For example
public class Main {
private int x = 3;
public static void main(String[] args) {
// Not possible
System.out.println(x);
}
}
A variable defined without the keyword static
is named an instance variable and can be accessed only from an instance method (a method defined without the keyword static
).

Davide Lorenzo MARINO
- 26,420
- 4
- 39
- 56