0

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!

Community
  • 1
  • 1

2 Answers2

2

I don't think you need any static context here other than your main() method. Instead you should ask each Person if they want a book e.g.

List<Person> persons = ... // and populate your list
for (Person p : persons) {
   if (p.wantsBook(book)) {
      ...
   }
}

The key to OO is that you tell objects to do things for you, not extract info and do it yourself.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • @NeilLocketz Syntax is right above. – Subhrajyoti Majumder Jan 29 '13 at 13:44
  • Thanks! That seems very reasonable. The problem is that i need to do this in a separate method that only takes Book b as a parameter, and then call it from main. However, i only call the method after i have made the necessary objects, but the compiler does not agree. Thanks :) – user2005142 Jan 29 '13 at 14:10
0

It looks like u were asked to do strange thing. If I understand u well you were asked to have a static method in the class and have this method iterating trought your Person objects and do 'something' with them. Tou were asked as well that the static doesAnybodyWantIt takes only a book as parameter. If this is what u were asked for (what is not a very nice thing to do) the only option i can see is to have a static List attribute on the class that would store your Persons. That static list should be avilable from insiade of the static doesAnybodyWantIt method so u would have acces to your persons without passing them in method parameters.

rgasiore
  • 140
  • 7
  • Yes, doesAnybodyWantIt takes only a book as parameter. It says i should call it from main() and do some methods on four already defined Person-objects within doesAnybodyWantIt(Bok b). If i try to make every single method and variable in the program static i lose the error. but the, of course, nothing else works because of the static variables :) Thanks! – user2005142 Jan 29 '13 at 14:12