2 Replies - 615 Views - Last Post: 03 November 2011 - 01:55 PM Rate Topic: -----

#1 siriusfeynman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-November 11

calling a method in for loop

Posted 03 November 2011 - 01:27 PM

first of all I'm very new to java


anyway the code:
public  void setScore(int mod )
    {
      System.out.println("the number of credits recieved is: ");
       mark = mod;
      System.out.println( mark );
    }

public void modifyCredit()
    {
        for(Student student : students) {
            LabClass.setScore(mod); 
            student.c = student.c + mod;

        }
    }



gives me the error:
non static method score(int) cannot be referenced from a static context

what I'm trying to do is have one method (setScore)score for inputting a credit modifier (i.e. losing or gaining a certain number of credits) and another method (modifyCredit) calling that method for each student in the class

am I going about this the right way?
(I'm using blueJ if that's important)

Is This A Good Question/Topic? 0
  • +

Replies To: calling a method in for loop

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: calling a method in for loop

Posted 03 November 2011 - 01:47 PM

Not easy without seing all your code but I presume tha LabClass is your class
To call setScore() you need a LabClass object

LabClass lc = new LabClass(....whateever the constructor requires)
// now you can call the setScore() method that will reference the instance variables of that object
lc.setScore(n);
Was This Post Helpful? 1
  • +
  • -

#3 siriusfeynman   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 03-November 11

Re: calling a method in for loop

Posted 03 November 2011 - 01:55 PM

thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1