Possible Duplicate:
calling non-static method in static method in Java
I am running into the "non-static method cannot be referenced from a static context"-error when i try to call a method that involves already defined objects from my main-function.
I have three classes: a Person-class, a Book-class and a third class that contains the main-method.
Now, i have been asked to make four Person-instances inside main(), and to define a method "doesAnybodyWantIt(Book b)" that asks the four Persons if they want the book. It would look something like this: doesAnybodyWantIt(Book b){ me.doIwantIt(b); lisa.doIwantIt(b).. with the four person-objects).
So as i was afraid, javac does not know that the four Person-objects exist and raises the error. I know that i could instead write the method as doesAnybodyWantIt(Book b, Person a, ... ), but this is not what i am asked.
So my question is: is there a way to side-step this problem without passing the Persons as parameters to the doesAnybodyWantIt
-method? I have tried to include Person me = new Person(..)
in the constructor method with no luck.
In this type of setup, where i want to do specific things to objects that i know i will define, how do i sidestep the error?
I am not sure wether i should include the original source code or not; all the methods and parameters are given Norwegian names, so it would not be very readable i fear.
Thank you all so much! Marius
Thanks for the replies! You are of course right, i have to include some code to this messy question.
class Error{
private Person jeg;
public void doesAnybodyWantIt(Bok b){ //This method has to be declaired withink the classm, and can only take the Bok b as a parameter.
Bok bok = jeg.vilJegHaBoka(b);
// loops over the four persons in question.
}
public static void main(String[] args){
Person jeg = new Person("Jeg", "java");
Person lisa = new Person("Lisa", "crime");
Person ramiz = new Person("Ramiz", "food");
Person emil = new Person("Emil", "sports");
doesAnybodyWantIt(new Bok("java"));
}
}
class Bok{
private String kat;
Bok(String k){ //the category of the book is assigned to kat as a string.
kat = k;
}
public String kategori(){return kat;} //returns the category.
}
class Person{
private String navn; //name
// private Person bestevenn; non-relevant
private String bokKat; // The category of books that is at interest to the person.
Person(String navnet, String kat){ //sets the name and category of books.
navn = navnet;
bokKat = kat;
}
private boolean mittInteressefelt(Bok b){ //Tests if the book's category is the same as my interest.
if (bokKat.equals(b.kategori())){ //Returns true if interested. else false.
return true;
}
else{
return false;
}
}
public Bok vilJegHaBoka(Bok b){ //A method to decide if the book will be kept or not. Returns null if i want the book, and the book otherwise.
if (mittInteressefelt(b) == true){ return null; }
else { return b;}
}
}
A person has a name and field of interest, and a book has a category. The assignment says that i should make a method vilNoenHaBoka (= doesAnybodyWantTheBook) that loops over the four specific Person-objects that are created in main, and ask each of them if they want the book. It also states that the method should look like public void vilNoenHaBoka(Bok b). Now, i feel really bad about asking this clearly assignment-related question, and if this in inappropriate, please let me know. I am however just curious to find out what a good way would be to go about such a problem in general - should i pass the Person-objects to vilNoenHaBoka() for instance?
Thanks again!