Results 1 to 8 of 8
  1. #1
    GRoss is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Question 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:

    PHP 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!");
    
    	}
    //-------------------------------------------------------------------------------------
    I also have a method in the CourseAdmin class, returning the unitList ArrayList

    PHP Code:
    public ArrayList<Unit> getUnitList()
    	{
    		return unitList;
    	}
    ...and the Main method from the CourseAdmin
    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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  3. #3
    GRoss is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    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:
    PHP Code:
    CourseAdmin driver = new CourseAdmin();
    "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.

    Do you think you can give me an example or something?

    ..I don't get it :(

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  5. #5
    ujjal.ruet is offline Member
    Join Date
    Apr 2010
    Posts
    9
    Rep Power
    0

    Default

    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.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by ujjal.ruet View Post
    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.
    Unfortunately that has nothing to do with the OP's problem. See my previous explanation for what the problem is and the possible solutions available.

  7. #7
    GRoss is offline Member
    Join Date
    May 2010
    Posts
    8
    Rep Power
    0

    Default

    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!

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

Similar Threads

  1. Replies: 3
    Last Post: 02-11-2010, 09:59 AM
  2. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  3. Replies: 11
    Last Post: 11-19-2008, 03:13 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
  •