SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27
  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question non-static method in static context error (was "Please help pretty quickly")

    I would prefer if this could be answered pretty quickly as I ahve a deadline to meet for tommorow morning and need to get it working for then.


    I ahve a class called Controller and at the top I ahve:

    Code Java:
    import java.util.Scanner;
    public class Controller extends Student

    and the bit that it is complaing about is:

    Code Java:
                    setyear(studentYear);
     
                    setcourse(studentCourse);


    My class called Student has this code:

    Code Java:
            public void setcourse(String s)
            {
                course = s;
            }
     
            public void setyear(int yr)
            {
                year = yr;
            }

    I am getting this error when trying to compile controller:

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

    I really do not know how to resolve this and hope someone here can advise

    Thanks

    Regards,
    Neil

  2. #2
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    hm... I'm guessing you're invoking those method w/o an instance of a class.

    Did you do

    Controller controller = new Controller();
    int studentYear = 0;
    controller.setyear(studentYear);

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not usre what you meant there but below is my Controller Class:

    Code Java:
    import java.util.Scanner;
    public class Controller extends Student
        {
            //public static DateNClock aClock, anotherClock;
            public static void main (String args[])
            {
                //aClock = new DateNClock ();
                //aClock.setDate(12,11,02);
                //aClock.setTime(10,0,0);
                //System.out.println(aClock);           // inherited toString() is called
                //anotherClock = new DateNClock();
                //anotherClock.copy(aClock);            // uses inherited copy method
                //System.out.println(anotherClock);
     
                //Prints a space to bring up Terminal Window - (Stops Confusion)
                System.out.println(" ");
     
                //Initiates the scanner
                Scanner myScanner = new Scanner(System.in);
     
                //While '1' has been pressed on the keyboard then allow a person to be created
                int ans = 2;
                System.out.println("Please press '1' to enter a new person, '2' to enter a Student or '3' to exit:");
                ans = myScanner.nextInt();
     
                while(ans == 1) {
     
                    //Dummy input line to fix a java bug
                    String dummy=myScanner.nextLine();
     
                    //Sets up person details
                    System.out.println("Please enter the persons name:");
                    String personName = myScanner.nextLine();
     
                    System.out.println("Please enter the persons gender - (m or f):");
                    char personGender = myScanner.nextLine().charAt(0);
     
                    System.out.println("Please enter the persons address:");
                    String personAddress = myScanner.nextLine();
     
                    System.out.println("Please enter the persons NIN:");
                    String personNIN = myScanner.nextLine();
     
                    System.out.println("Please enter the persons age:");
                    int personAge = myScanner.nextInt();
     
                    //Sets up date details
                    System.out.println("Please enter DOB day:");
                    int dobDay = myScanner.nextInt();
     
                    System.out.println("Please enter DOB month:");
                    int dobMonth = myScanner.nextInt();
     
                    System.out.println("Please enter DOB year:");
                    int dobYear = myScanner.nextInt();
     
                    Date aDate = new Date(dobDay, dobMonth, dobYear);
     
                    Person aPerson = new Person(personName, personGender, personAge, personAddress, personNIN, aDate);
     
                    //Prints out last person entered
                    System.out.println(aPerson);
                    System.out.println("Please press '1' to enter a new person or '2' to exit:");
                    ans = 2;
                    ans = myScanner.nextInt();
                }
     
                while(ans == 2) {
     
                    //Dummy input line to fix a java bug
                    String dummy=myScanner.nextLine();
     
                    //Sets up student details
                    System.out.println("Please enter the students name:");
                    String studentName = myScanner.nextLine();
     
                    System.out.println("Please enter the students gender - (m or f):");
                    char studentGender = myScanner.nextLine().charAt(0);
     
                    System.out.println("Please enter the persons address:");
                    String studentAddress = myScanner.nextLine();
     
                    System.out.println("Please enter the students NIN:");
                    String studentNIN = myScanner.nextLine();
     
                    System.out.println("Please enter the students age:");
                    int studentAge = myScanner.nextInt();
     
                    //Sets up date details
                    System.out.println("Please enter DOB day:");
                    int dobDay = myScanner.nextInt();
     
                    System.out.println("Please enter DOB month:");
                    int dobMonth = myScanner.nextInt();
     
                    System.out.println("Please enter DOB year:");
                    int dobYear = myScanner.nextInt();
     
                    System.out.println("Please enter the students course:");
                    String studentCourse = myScanner.nextLine();
     
                    System.out.println("Please enter the students year:");
                    int studentYear = myScanner.nextInt();
     
                    Date aDate = new Date(dobDay, dobMonth, dobYear);
     
                    Student aStudent = new Student(studentName, studentGender, studentAge, studentAddress, studentNIN, aDate);
     
                    setyear(studentYear);
     
                    setcourse(studentCourse);
     
                    //Prints out last student entered
                    System.out.println(astudent);
                    System.out.println("Please press '1' to enter a new person or '2' to enter a new student or '3' to exit:");
                    ans = 3;
                    ans = myScanner.nextInt();
                }
     
                    //Display Counter
                    System.out.println("Number of new people/students created: " + count());
     
                    //Display Thanks message
                    System.out.println("Thanks! - You have been evicted!");
            }
        }


    and here is my student clas:

    Code Java:
    public class Student extends Person
        {
            //Declares variables
            private int year;
            private String course;
     
     
            //Creates a new default student
            public Student()
            {
                super();
                year = 0000;
                course = "unknown";
            }
     
     
            public Student(String nme, char sex, int howOld, String addr, String NIN, Date dob)
            {
                super (nme, sex, howOld, addr, NIN, dob);
            }
     
            public void setcourse(String s)
            {
                course = s;
            }
     
            public void setyear(int yr)
            {
                year = yr;
            }
     
            public String toString()
            {
                return super.toString()+ " Course: " +course + "Year: " + year;
            }
     
            public Student (Student other)
            {
                super (other);
                course=other.course;
                year=other.year;
            }
     
            public void copy (Student other)
            {
                course = other.course;
                year = other.year;
            }
     
        }

  4. #4
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please help asap if you can, and please try to keep it simple!

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Since this is a class project I won't spoon feed you the answer. Look at my post earlier and focus on this
    Code:
    Student aStudent = new Student(studentName, studentGender, studentAge, studentAddress, studentNIN, aDate);
                   
    setyear(studentYear);
                   
    setcourse(studentCourse);

  6. #6
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm still completely confused.

    I have tried Student.setyear(studentYear); and also:

    Controller.setyear(studentYear);

    and both of those come to no avail

    I don;t know what you mean by this:

    Controller controller = new Controller();

    but I am running this is BlueJ and to run the Controller Class I right clcik on the Controller class showing in the main UML digarm and click public static void main to start it all off.

    Please help some more. or at least some more easier to decipher hints

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    rctneil, be honest. Did you sleep through the class?

  8. #8
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, yes, I'll tell the truth, I fell asleep. but i still don't see why it is not working. Is it something silly or not?

  9. #9
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    very very very silly

  10. #10
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    give you another hint

    Student aStudent = new Student(studentName, studentGender, studentAge, studentAddress, studentNIN, aDate);

    Why did you write this line? maybe this will give you the answer

  11. #11
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Everything I try just comes back saying cannot find symbol - class setyear!!!!!!!!!!!

  12. #12
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Alright, this my last last hint, I just can't give out more than this
    Code:
    Controller controller = new Controller();
    int studentYear = 0;
    controller.setyear(studentYear);
    Code:
    Student aStudent = new Student(studentName, studentGender, studentAge, studentAddress, studentNIN, aDate);
                   
    setyear(studentYear);
                   
    setcourse(studentCourse);
    Also, by looking at
    Code:
    System.out.println(astudent);
    If you can write above statement then, you must know what I'm talking about. Anyways, my last post. G'luck.

  13. #13
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help. I really am stuck and have just tried loads of ideas but none of them seem to work. I do appreciate your help but none of your hints seem to click.

    I don;t know if your trying to hint at something I don;t know about in Java yet or if it is something else.

    I have looked over your hints again and my ideas were:

    new setYear(blah);

    or

    Student.setYear(blah);

    things like that. No ideas are coming to me at all

    anyway, you said those were your 3 only hints so thanks again.

    Regards,
    Neil

  14. #14
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I ahve also tried adding studentYear and studentCourse onto the end of the new Student method but that fails as that exact method does not exist. I must use the setYear and setCourse thingies

  15. #15
    SitePoint Wizard
    Join Date
    Apr 2007
    Posts
    1,381
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Alright, I'll give a point for trying but your problem is trying to solve a problem before you can understand the concept. Take a deep breath and read this slowly, it won't take you more than 30 minute.

    http://www.javabeginner.com/java-class-objects.htm

  16. #16
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rctneil View Post
    non-static method setyear(int) cannot be referenced from a static context
    Let's break this down:

    non-static method cannot be referenced from a static context

    This means exactly what it says. A 'static context' is one that has not been instantiated, but simply 'exists'. public static void main(...) should sound familiar.

    A non-static item is one that must be part of an Object that has been instantiated. How to know if something is non-static? It doesn't have 'static' in front of it.

    In your case, the method setYear(int) is non-static, as such, it doesn't exist until the class that contains has been instantiated (SomeClass class = new SomeClass() 'instantiates' a SomeClass Object).

    Running a class does not instantiate it, the Class Loader simply invokes the main method, which 'exists' because it has been declared as 'static'. The compiler knows this and is telling you that you cannot call a method on an Object that hasn't been created yet.

    Moral of the story is: you haven't created a new Student to work with, so the code, literally, has nothing to call methods on.

  17. #17
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just read through the beginning of that document and saw that to reference a member function you use the class name then a dot and then the function name like so:

    Student.setYear(blah);

    but that just fails and says it cannot be found!

  18. #18
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    'that document'...what document?

  19. #19
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought it could run the setYear class because I have called it after creating the new student as below:

    Code Java:
                    //Create a new student
                    Student aStudent = new Student(studentName, studentGender, studentAge, studentAddress, studentNIN, aDate);
     
                    Student.setYear(studentYear);

    Quote Originally Posted by rushiku View Post
    Let's break this down:

    non-static method cannot be referenced from a static context

    This means exactly what it says. A 'static context' is one that has not been instantiated, but simply 'exists'. public static void main(...) should sound familiar.

    A non-static item is one that must be part of an Object that has been instantiated. How to know if something is non-static? It doesn't have 'static' in front of it.

    In your case, the method setYear(int) is non-static, as such, it doesn't exist until the class that contains has been instantiated (SomeClass class = new SomeClass() 'instantiates' a SomeClass Object).

    Running a class does not instantiate it, the Class Loader simply invokes the main method, which 'exists' because it has been declared as 'static'. The compiler knows this and is telling you that you cannot call a method on an Object that hasn't been created yet.

    Moral of the story is: you haven't created a new Student to work with, so the code, literally, has nothing to call methods on.

  20. #20
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rushiku View Post
    'that document'...what document?
    the one sg707 linked to (not a document sorry, webpage)

  21. #21
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Look, I know I am really bad at this, but I am desperate to get this one part working tonight. I'm sure it is something simple which I am just not understanding but I just do not get it!

  22. #22
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by rctneil View Post
    I thought it could run the setYear class because I have called it after creating the new student
    Oh, hehehe, (sorry) you're so close!

    'Student' still refers to the Class Student.

    'aStudent' is the name of the Student Object you created, this is the one you want.

  23. #23
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I have also tried using aStudent.setYear(int); and that fails to with this error:

    cannot find symbol - method setYear(int)

  24. #24
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    As Java is case sensitive setYear(int) != setyear(int)

    If you're not familiar with it != means 'not equal'

    also, setYear is the 'correct' way to name your methods and variables (always start with a lower case letter, capitalize additional words)

  25. #25
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks. So basically it was the incorrect case in Student that was causing the problems and I did try aStudent.setYear(int);

    and as it failed then i just bypassed it and tried something else.

    Thanks guys


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •