Results 1 to 8 of 8
- 05-18-2010, 10:11 AM #1
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Non-static method cannot be referenced from a static context
Hi everybody!
I am currently studying Java at uni, and working on my assignment. Everything was going good, until I got stuck :(
I am supposed to produce a student enrolment system which allows students to be enrolled into courses, units, it also allows creating new students/courses/units and modifying all of them.
I have 4 classes so far:
CourseAdmin (Main Class), Course, Student, Unit.
Basically what I am trying to do is get the unitList ArrayList from the main (CourseAdmin) class, check if the particular student has any spare unit slots, check if the unit name supplied matched a unit in the uniList in the CourseAdmin class and if all of these are good, then enrol the student in the unit matching the unit code supplied
The method, which I got stuck on is in Student class - here's the code:
I also have a method in the CourseAdmin class, returning the unitList ArrayListPHP Code://------------------------------------------------------------------------------------- //Enrolls a student into a unit, checking if the student has at least 1 empty //slot in the enrolledUnits array and then checking if the unitCode supplied //matched the unitCode of a unit in the CourseAdmin class //If error occurs - the method will inform the user public void enrollInUnit(String unitCode) { int i = 0; //For student's unitList int i2 = 0; //for Enrollment system's unitList while(i <= (enrolledUnits.length - 1) && enrolledUnits[i] != null) { i++; } while(!CourseAdmin.getUnitList().get(i2).getUnitCode().equals(unitCode) && i2 <= CourseAdmin.getUnitList().size()) { i2++; } //Makes sure that there is at least 1 empty unit slot for the unit if(enrolledUnits[i] == null) { if(CourseAdmin.getUnitList().get(i2).getUnitCode().equals(unitCode)) enrolledUnits[i] = CourseAdmin.getUnitList().get(i2); else System.out.println("No such unit in the database!"); } else System.out.println("This student is already enrolled into 4 units!"); } //-------------------------------------------------------------------------------------
...and the Main method from the CourseAdminPHP Code:public ArrayList<Unit> getUnitList() { return unitList; }
PHP Code:public static void main(String[] args) { CourseAdmin driver = new CourseAdmin(); driver.menu(); }Last edited by GRoss; 05-18-2010 at 10:17 AM.
- 05-18-2010, 11:29 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Is the getUnitList method a utility method? If so then it should be made static.
Otherwise your Student needs to have an instance of CourseAdmin to be able to call the non static method getUnitList.
- 05-18-2010, 12:11 PM #3
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
I am not quite sure what is an utility method, but after I have made it static, the number of errors went down to only 1 - the return statement of the getUnitList method (Non-static method cannot be referenced from a static context).
I don't really understand what you mean by "your Student needs to have an instance of CourseAdmin" - the whole static/non-static issue has been a problem for me for a while and I still don't quite get it.
I understand that in saying:
"driver" becomes the instance of the class CourseAdmin, but I am not quite sure how would I use this for my problem, since as far as I understand if I create a new object of CourseAdmin type in the Student class, its values will no be the same as of another object in the main class.PHP Code:CourseAdmin driver = new CourseAdmin();
Do you think you can give me an example or something?
..I don't get it :(
- 05-18-2010, 12:26 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
A utility method could be defined as one which still make sense even when moved to a different class (typically because it doesn't use any instance variables or methods). e.g A method that gives you the area of a circle when passed the circle's radius makes sense in a class called MathUtils or TrigUtils. It only uses it's arguments to calculate the result.
A method called setName however, only makes sense in a class that has an instance variable called name. So if a method uses instance variables of a class it cannot be made static. It is not a utility.
More technically, statics are resolved first (e.g static variables are initialized when their class is loaded). Instance variables are only initialized when an instance of their class is created (typically when the constructor is invoked). This happens after the class has been loaded. This is the reason why statics are generally not allowed to access instance variables/methods, because the associated objects do not exist yet when the class is loaded.
Your getUnitList method returns an instance variable (unitList). So it can't be static.
Anyone who wants to call the getUnitList method needs to have an instance of CourseAdmin to be able to call the getUnitList method. If it doesn't make sense to enforce such a requirement then the unitList variable should also be static allowing the getUnitList method to be able to be static as well.
- 05-18-2010, 10:37 PM #5
Member
- Join Date
- Apr 2010
- Posts
- 9
- Rep Power
- 0
Though I am a begineer of JAVA,I can give you a probable solution of it.
I have faced problem same as you.I got relief from the solution given below...
Make a constructor of your classname.java outside of your main() method.Then call the method getUnitList() from your constructor.
An object of that class must be created in your main() method.
- 05-19-2010, 07:16 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 05-19-2010, 11:07 AM #7
Member
- Join Date
- May 2010
- Posts
- 8
- Rep Power
- 0
Thanks for your replies - spent the whole evening trying to get this. So what you mean is that unitList must be declared as static along with getUnitList() method, because if they aren't static, the enrollInUnit method in the student class will not know which instance of CourseAdmin Class to look into for the values required.
However if both unitList and getUnitList() are declared as static, the CourseAdmin Class will only have 1 copy of unitList variable, so the student class can go ahead and access this variable, since there is only 1 copy of it, regardless of how many CourseAdmin instances there are.
This sort-of makes sense to me.. I hope that I am correct in what I have said.
Thanks again!
- 05-19-2010, 11:12 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
non-static method cannot be referenced from a static context.
By blackstormattack in forum New To JavaReplies: 5Last Post: 05-07-2009, 04:05 AM -
Method cannot be referenced from a static context - HELP
By jmorris in forum New To JavaReplies: 11Last Post: 11-19-2008, 03:13 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks