0

I am having an issue with this error, while trying to write a method that lists all names in a specific class. (error at bottom) I have tried a few things but for the life of me, cannot figure it out. Please help, thanks.

Class Cat:

public class Cat
{
// instance variables 
private String name;
private int yearOfBirth;
private int weightInKilos;

public Cat() {
   setName("");
   setYearOfBirth(0);
   setWeightInKilos(0);
}

/**
 *  
 */
public Cat(String newName, int newYearOfBirth, int newWieghtInKilos )
{
    setName(newName);
    setYearOfBirth(newYearOfBirth);
    setWeightInKilos(newWieghtInKilos); 
}


public String getName(){
    return name;
}

public int getYearOfBirth(){
    return yearOfBirth;
}

public int getWieghtInKilos(){
    return weightInKilos;
}



public void setName(String newName){
    if (newName != null ){
    name = newName;
    }
    else{
        System.out.println("Invalid Name");
    }

}

public void setYearOfBirth(int newYearOfBirth){
    if (yearOfBirth >= 0){
    yearOfBirth = newYearOfBirth;
    }
    else{
        System.out.println("Year Of Birth must not be negative!");
    }
}

public void setWeightInKilos(int newWeightInKilos){
    if (weightInKilos >= 0){
    weightInKilos = newWeightInKilos;
    }
    else{
        System.out.println("Weight must not be negative!");
    }

}

}

Class Cattery:

import java.util.ArrayList;


public class Cattery
{
// instance variables - replace the example below with your own
private ArrayList <Cat> cats;
private String businessName;

/**
 * Constructor for objects of class Cattery
 */
public Cattery(String NewBusinessName)
{
    cats = new ArrayList <Cat>();
    NewBusinessName = businessName;
}

public void addCat(Cat newCat){

    cats.add(newCat);
}

public void indexDisplay(int index) {
    if((index >= 0) && (index <= cats.size()-1)) {
        System.out.println(index);
    }
    else{
        System.out.println("Invalid index position!");
    }
}

public void removeCat(int indexremove){
     if((indexremove >= 0) && (indexremove <= cats.size()-1)) {
         cats.remove(indexremove);
        }
    else{
        System.out.println("Invalid index position!");
    }
}

public void displayNames(){
   System.out.println("The current guests in Puss in Boots Cattery:");
   for(Cat catNames : cats ){
       System.out.println(Cat.getName());   //ERROR; non static method cannot be referenced from a                  static context..wtf

}
}
}

Please help, thanks

Joshua Baker
  • 401
  • 1
  • 7
  • 15

4 Answers4

3

When you have an instance method, you need to call it on a specific instance of the class.

Here:

System.out.println(Cat.getName());

you're trying to call it on the Cat class itself. You want:

for (Cat cat : cats ) {
    System.out.println(cat.getName());
}

Note that I've changed the name of the iteration variable from catNames to cat as well - because the value is just a reference to "the cat we're looking at at the moment". It's not the cat name, nor is it multiple cats (or cats names) - it's a single cat. It's very important to name variables carefully - it can help correct code to look correct, and incorrect code to look incorrect. It doesn't make sense to call getName() on a variable called catNames... (what is the name of a collection of names?) but it absolutely makes sense to call it on a variable called cat.

Another warning bell from your original code was that the body of your for loop didn't use the iteration variable - that almost always suggests that something's wrong. The fixed version does, of course.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks that worked, and I understand more now. I have another question. My public void indexDisplay does not work. I want it to print out the name of my other class, instead it just prints out the parameter I have set. I think I need to get the 'index' to equal something, but I'm having trouble with this as well. Any suggestions? – Joshua Baker Mar 04 '13 at 08:40
  • @JoshuaBaker: If you have another question, you should ask it *as* another question - ideally with a short but complete program which *only* shows the problem. – Jon Skeet Mar 04 '13 at 08:41
2

Use:

System.out.println(catNames.getName()); 

getName is non static function, so you need to use it on an instance of that class, like you have in the cats list.

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
0
for (Cat cat : cats) {
    System.out.println(Cat.getName());
}

Here you need to use cat, not Cat. So use

for (Cat cat : cats) {
    System.out.println(cat.getName());
}
Michael Xin Sun
  • 1,174
  • 1
  • 10
  • 12
PSR
  • 39,804
  • 41
  • 111
  • 151
0
Cat.getName() this line means getName() should be static method in Cat class, but's not as such.

so access getName() method by instacne .

System.out.println(catNames.getName()); 
NPKR
  • 5,368
  • 4
  • 31
  • 48