Results 1 to 2 of 2
  1. #1
    oceansdepth is offline Member
    Join Date
    Mar 2008
    Rep Power

    Default Help with code (static error)

    I have two classes: farm and cow. Class cow compiled fine but class farm is giving me this error:

    1) " non-static method buildCow() cannot be referenced from a static context"

    2) I also need to write a for loop to report on the herd after I loaded my array.

    3) I also have to write a heading, and use the toString method to report, then write a summary.

    I really mostly need help on number 1. The rest I'm trying to work on now... Thank you.


    import java.util.*;
    import java.lang.*;

    public class cow

    private String name;
    private String breed;
    private int weight;
    private char gender;

    //define the class constructor
    public cow()
    { = "";
    this.breed = "";
    this.weight = 0;
    this.gender = ' ';
    //pre: String newname, String newbreed, int newweight, char newgender
    //post: void (nothing)

    public cow (String newname, String newbreed, int newweight, char newgender)
    name = newname;
    breed = newbreed;
    weight = newweight;
    gender = newgender;


    //pre: object cow
    //post: String name

    public String getName()
    return name;
    }//***************************end of getName

    //pre: object cow
    //post: String breed
    public String getBreed()

    return breed;
    }//***************************end of getBreed

    //pre: object cow
    //post: int weight
    public int getWeight()
    return weight;
    }//***************************end of getWeight

    //pre: object cow
    //post: char gender
    public char getGender()
    return gender;
    }//**************************end of getGender

    //pre: object
    //post: String description

    public String toString()

    String description = name+ " is a " + breed + " cow that weighs " +weight+ " lbs. Gender: " + gender;
    return description;
    }//*************************end of toString

    //pre: String newName
    //post: nothing

    public void setName(String newName)
    name = newName;
    }//*************************end of setName
    //pre: String newBreed
    //post: nothing
    public void setBreed(String newBreed)
    breed = newBreed;
    }//*************************end of setBreed

    //pre: int newWeight
    //post: nothing
    public void setWeight(int newWeight)
    weight = newWeight;
    }//*************************end of newWeight

    //pre: char newGender
    //post: nothing
    public void setGender(char newGender)
    gender = newGender;
    }//*************************end of newGender

    //pre: nothing
    //post: cow
    public cow buildCow ()
    Scanner cscan = new Scanner(;
    cow c1 = new cow();
    System.out.println("What is the name of the cow?: ");
    name = cscan.nextLine();
    System.out.println("What is the breed of the cow?: ");
    breed = cscan.nextLine();
    System.out.println("What is the weight of the cow? ");
    weight = cscan.nextInt();
    System.out.println("What is the gender of the cow? ");
    gender = cscan.nextLine().charAt(0);


    return c1;


    import java.util.*;
    import java.lang.*;
    public class farm

    Scanner cscan = new Scanner(;
    int newcows = 0;
    public cow cowList()
    System.out.println("How many cows do you wish to create?: ");
    newcows = cscan.nextInt();

    cow[] cowList = new cow[newcows];
    for(int index = 0; index < newcows; index ++)
    cow c1 = cow.buildCow();


    i highlighted in red what gave me the prob.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Java Code:
    cow c1 = new cow();
    cow c2 = c1.buildCow();
    // or
    cow c = new cow().buildCow();

Similar Threads

  1. Replies: 0
    Last Post: 04-16-2008, 11:07 PM
  2. Pls help with a code error.
    By saytri in forum New To Java
    Replies: 8
    Last Post: 12-24-2007, 09:10 PM
  3. error in code
    By dirtycash in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 12:40 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts