Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    Hi, I have a super class Person and its sub class Student. Student has a toString method that prints out the details of the Student object including the first name. But I keep getting the error described in the title. Is there another way to fix this problem without making the method static?

    Person super class
    public class Person
    {
        private String firstName, lastName, id;
     
        /**
         * Creates a Person object.
         * 
         * @param firstName the first name of the Person
         * @param lastName the last name of the Person
         * @param id the id number of the Person
         */
        public Person(String firstName, String lastName, String id) {
            this.firstName=firstName;
            this.lastName=lastName;
            this.id=id;
        }
     
        /**
    	 * Returns the first name.
    	 * 
    	 * @return the firstName
    	 */
    	public String getFirstName() {
    		return firstName;
    	}
     
    	/**
    	 * Sets the first name.
    	 * 
    	 * @param firstName the firstName to set
    	 */
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
     
    	/**
    	 * Returns the last name.
    	 * 
    	 * @return the lastName
    	 */
    	public String getLastName() {
    		return lastName;
    	}
     
    	/**
    	 * Sets the last name.
    	 * 
    	 * @param lastName the lastName to set
    	 */
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
     
    	/**
    	 * Returns the id.
    	 * 
    	 * @return the id
    	 */
    	public String getId() {
    		return id;
    	}
    }

    Student sub class:
     
    import java.util.ArrayList;
     
     
    public class Student extends Person {
     
    	private double gpa;
    	private ArrayList<CourseAttempted> transcript;
    	private int creditsCompleted;
     
    	/**
    	 * Creates a student.
    	 * 
    	 * @param firstName the first name of the student
    	 * @param lastName the last name of the student
    	 * @param id the student's id
    	 */
    	public Student(String firstName, String lastName, String id) {
    		super(firstName, lastName, id);
    		gpa = 0.0;
    		transcript = new ArrayList<CourseAttempted> ();
    		creditsCompleted = 0;
    		}
     
    	/**
    	 * @return the gpa
    	 */
    	public double getGpa() {
    		return gpa;
    	}
     
    	/**
    	 * Adds a course to the student's transcript.
    	 * 
    	 * @param courseToAdd the course to add
    	 */
     
    	public void addCourse(Course c, String semester, int year, double grade) {
    	    CourseAttempted courseToAdd = new CourseAttempted(c, semester, year, grade);
    	    transcript.add(courseToAdd);
     
    		// the total number of credits attempted is the sum of
    		// creditsCompleted and the number of credits of the course to be added
    		// to the transcript
    		int creditsAttempted = courseToAdd.course.getCredits() + creditsCompleted;
     
    		// gpa is computed by doing the following:
    		// for each course attempted, multiply the number of credits by the
    		// course grade.  
    		// gpa is the sum of the above for all the courses divided by the
    		// number of total credits
    		// 
    		double currentPoints = gpa * creditsCompleted + courseToAdd.grade * 
    		                        courseToAdd.course.getCredits();
     
    		gpa = currentPoints / creditsAttempted;
     
    		if (courseToAdd.grade > 0) 
    		   creditsCompleted += courseToAdd.course.getCredits();
    	}
     
    	/**
        * Returns the list of courses as a String.
        * 
        * @return the list of courses the student has taken
        */
        public String getTranscript() {
            String courseList = "";
     
            for (CourseAttempted ca: transcript) 
                courseList = courseList + ca + "\n";
     
            return courseList;
        }
     
    	/**
    	 * Returns a String representation of a Student
    	 * 
    	 * @return a string representing the student
    	 */
    	public String toString() {
    		String s = this.getClass().getName().toUpperCase() + ":\t" + Person.getFirstName() + " " + lastName 
    				+ "\tID = " + id + "\tGPA = " + gpa + "\tCredits Completed = " + creditsCompleted;
    		s += "\nHere are all the courses the student has taken so far";
    		s += "\n-----------------------------------------------------";
    		s += "\n" + getTranscript();
            return s;
        }	
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    A toString() method definitely should not be static.

    When you call the toString() method, use a reference to the class, not the name of the class.
    Using the class's name means it's a call to a static method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    I'm talking about changing the getFirstName() method to static not the toString() method. I cant call the toString() because I can't even compile the student class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    Don't make any of the methods static. Use references to the class object to call a class's methods.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    So are you saying I should create a person object within my Person class to be able to call methods from that class?
    Like:
    Person p = new Person("Joe", "Smith", "0394857");

    To call p.getFirstName();?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    You do NOT need a reference to the class to call other methods of the same class. There is an implied this reference added by the compiler. Just code the method name without any object reference.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    EDale (May 3rd, 2013)

  8. #7
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error message non-static method Person.getFirstName() cannot be referenced from a static context?

    I got it. Thank you so much! This was my last programming assignment of the semester. So glad it's over!

Similar Threads

  1. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  2. non static varible from static context error
    By chopficaro in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2012, 07:07 PM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM