0
public class Thing
{
public static void main (String[] args)
    {
    //I set my veriables and prompt user to enter number of names
    //Prompt user for First and Last name and Store names in an array of classes

    Arrays.sort(listOfName, new Comparator<otherClass>()//Sort names according to last name
    //code here for that (which i have but not the issue)
    for (int i = 0; i<numName; i++)
        {
        ans = JOptionPane.showInputDialog(null,"How Many Students does: \n "+ listOfName[i]+ " Have");
        int val = Integer.parseInt(ans);
        otherClass.setnumStudents(val);// in my Separate class I have a setnumStudents
        }
    for(int i = 0; i<numName; i++)
        {
        System.out.println(listOfName[i]);
        }
    }

Earlier in my program I set my array to store according to the separate class otherClass and the int numStudents is set to 0 initially, after collecting firstName and lastName I then alphabetize the array and then inquire how many students each person has. Using setter setnumStudents I try to change the numStudents stored in the array. With my code as is I get this error:

non-static method setnumStudents(int) cannot be referenced from a static context
        otherClass.setnumStudents(val);
             ^              

Thank you for any help on this issue

  • a static method is shared between all instances of Thing, but nunStudents is an instance variable = a different one for each time a new thign is created. – NimChimpsky Mar 26 '13 at 16:40
  • If `otherClass` is a class name, you should first of all called it with an initial capital `O`. Secondly, you can call only methods which are static directly from a class name without instantiating an object. So, wither you make your `setnumStudents` static (which I doubt is what you really want) or you instantiate an object of type `otherClass` (possibly, `OtherClass`) – ThanksForAllTheFish Mar 26 '13 at 16:42
  • that's why you shouldn't be doing everything in the main method... – Jimmt Mar 26 '13 at 17:02

2 Answers2

1

I think you want:

listOfName[i].setnumStudents(val);

Or something along that line. Java coding conventions generally have class names at camel case starting with upper case. Is otherClass a class name? It should be OtherClass to avoid confusion.

I don't know if listofName[i] is the correct instance for 'listOfTutor[i]' but you need to find an instance of otherClass

Charles Forsythe
  • 1,831
  • 11
  • 12
  • Thank you, This compiles but doesn't actually change the values stored under the `otherClass`. and yes my actual class is capitalized but in sterilizing my question I forgot to correct that – Brad Jones Mar 26 '13 at 16:51
  • When you say "the values stored under the otherClass", you're talking about an instance member? Or is this a static (class-only) value? Can you post "otherClass" or at least the parts of it that are relevant? – Charles Forsythe Mar 26 '13 at 17:39
0

non-static method setnumStudents(int) cannot be referenced from a static context

means that you are accessing a non-static method using a class, while you are supposed to use an instance(object).

In the following

otherClass.setnumStudents(val);

is otherClass the name of the other class you are mentioning? In that case you need to instantiate this class and use the instance.

BTW, in Java class names generally start with an upper case letter by convention.

Nivas
  • 18,126
  • 4
  • 62
  • 76