I'm really confused with this! I have 2 classes, Club and Membership. In Membership I have the method, getMonth(), and in Club I have joinedMonth() which takes the parameter, 'month' - so a user enters a month and then I want it to return the Membership's which joined in that specific month.

I am trying to call the getMonth() method from class Club, so that I can then go on to compare the integers of the months. But, when I try to call the method, I just get the mentioned "non-static method getMonth() cannot be referenced from a static context".

Basically, what is this and how can I resolve it?

Thank you in advance!

Club:

public class Club
{
    private ArrayList<Membership> members;
    private int month;

    /**
     * Constructor for objects of class Club
     */
    public Club()
    {
        Membership member  = new Membership("John", 04, 2010);

    }

    /**
     * Add a new member to the club's list of members.
     * @param member The member object to be added.
     */
    public void join(Membership member)
    {
        members.add(member);
    }

    /**
     * @return The number of members (Membership objects) in
     *         the club.
     */
    public int numberOfMembers()
    {
        return members.size();
    }


        /**
    * Determine the number of members who joined in the given month
    * @param month The month we are interested in.
    * @return The number of members
    */
    public int joinedMonth(int month){

        Membership.getMonth();

    }



}

Membership:

public class Membership
{
    // The name of the member.
    private String name;
    // The month in which the membership was taken out.
    public int month;
    // The year in which the membership was taken out.
    private int year;

    /**
     * Constructor for objects of class Membership.
     * @param name The name of the member.
     * @param month The month in which they joined. (1 ... 12)
     * @param year The year in which they joined.
     */
    public Membership(String name, int month, int year)
        throws IllegalArgumentException
    {
        Membership member = new Membership("Josh", 5, 2011);
        if(month < 1 || month > 12) {
            throw new IllegalArgumentException(
                "Month " + month + " out of range. Must be in the range 1 ... 12");
        }
        this.name = name;
        this.month = month;
        this.year = year;
    }

    /**
     * @return The member's name.
     */
    public String getName()
    {
        return name;
    }

    /**
     * @return The month in which the member joined.
     *         A value in the range 1 ... 12
     */
    public int getMonth()
    {
        return month;
    }

    /**
     * @return The year in which the member joined.
     */
    public int getYear()
    {
        return year;
    }

    /**
     * @return A string representation of this membership.
     */
    public String toString()
    {
        return "Name: " + name +
               " joined in month " +
               month + " of " + year;
    }
}

You need a varable that refers to an instance of the Membership class that contains the method you are trying to call. The method and data belong to an instance of the class and are not static.

Membership.getMonth();

Replace Membership (the class name) with a variable that has the address of an instance of the class that you want to get the data from.

Replace Membership (the class name) with a variable that has the address of an instance of the class that you want to get the data from.

So could I just do month.getMonth(); ?

Does the month variable point to an instance of the Membership class?
Is month defined as a Membership? Was it assigned a value with something like this:
month = new Membership();

Hey, sorry I've now managed to sort out what I was doing. Figured I was going about it the complete wrong way! Thank you for your help still!

It looked confused. The method definition looked like a search was required.

Indeed! I figered I actually needed to iterate over all the Membership objects in the ArrayList. Then any which had the same 'month' integer as the given integer needed to be added to a new ArrayList, of which I could then return the size and display how many joined in that month! Here's what I did if anyone else were interested:

public int joinedMonth(int month){
        monthJoined = new ArrayList<Membership>();
        for (Membership member : members ){
            monthCheck = member.getMonth();
           if (monthCheck == month) {
               monthJoined.add(member);
           }
           else{
               System.out.println("No members joined in the given month");
            }     
        }
        return monthJoined.size();
        }

Thanks again for your responses Norm!

Seems like overkill to create an arraylist, add items to it then only return a count of the number of items added. Why not just count? Is monthJoined used anywhere else?

How many times does the "No members ..." message print out?

Oh really? I'm not sure how you use count. No monthJoined is used nowhere else.
Ahhh, yes I did wonder about that! It prints the amount of times corresponding to the amount of members if the entered month is a match, else it just prints the amount of times there are member overall! How would I display it just the once instead of the amount of members there are?

not sure how you use count.

Set count to 0 at start of method.
In the loop, Add 1 to count when a match found.

At end of loop, if count == 0 print the "No members.." message.
return count

Excellent! Thank you very much for that! I think I've done it correctly! It's working perfectly now.

    public int joinedMonth(int month){
        count = 0;
        for (Membership member : members ){
            monthCheck = member.getMonth();
           if (monthCheck == month) {
               count++;
            }
        }
        if(count==0){
            System.out.println("No members joined in this date");
        }
        return count;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.