Results 1 to 4 of 4
  1. #1
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Question non-static variable cannot be referenced from a static context

    Hello all,

    I am new to Java this is one of my assignment. When i tried to compile in command line it gave me the error: non-static variable cannot be referenced from a static context. Could someone shed some light for me on the concept and point me in the right direction? Much appreciated.

    Thank you.

    Java Code:
    import java.util.Scanner;
    
    
    public class StudentRecord 
    {
    
    	private int quiz1;
    	private int quiz2;
    	private int quiz3;
    	private int midterm;
    	private int finalExam;
    
    	private double numericScore;
    	private char letterGrade;
    
    	private final int A_GRADE = 90;
    	private final int B_GRADE = 80;
    	private final int C_GRADE = 70;
    	private final int D_GRADE = 60;
    
    	public StudentRecord (int quiz1, int quiz2, int quiz3, 
    		int midterm, int finalExam)
    	{
    		this.quiz1 = quiz1;
    		this.quiz2 = quiz2;
    		this.quiz3 = quiz3;
    		this.midterm = midterm;
    		this.finalExam = finalExam;
    	}
    
    /**********************************************************************************
     * These are the accessor methods, which is used to return the values of the 
     * private fields in class StudentRecord: quiz1, quiz2, quiz3, midterm, and final
     *********************************************************************************/
    	public int getQuiz1()
    	{
    		return quiz1;
    	}
    	
    	public int getQuiz2()
    	{
    		return quiz2;
    	}
    
    	public int getQuiz3()
    	{
    		return quiz3;
    	}
    	
    	public int getMidterm()
    	{
    		return midterm;
    	}
    	
    	public int getFinalExam()
    	{
    		return finalExam;
    	}
    	
    	public double getNumeriScore()
    	{
    		return numericScore;
    	}
    	
    	public char getLetterGrade()
    	{
    		return letterGrade;
    	}
    
    /**********************************************************************************
     * These methods are the mutator methods for class Student Records. These methods
     * are used to set the values of the private fields: quiz1/2/3, midterm and final.
     *********************************************************************************/
    	public void setQuiz1()
    	{
    		this.quiz1 = quiz1;
    	}
    	
    	public void setQuiz2()
    	{
    		this.quiz2 = quiz2;
    	}
    	
    	public void setQuiz3()
    	{
    		this.quiz3 = quiz3;
    	}
    	
    	public void setMidterm()
    	{
    		this.midterm = midterm;
    	}
    	
    	public void setFinalExam()
    	{
    		this.finalExam = finalExam;
    	}
    
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    
    		StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3, midterm, finalExam);
    
    		System.out.println("Enter the student's score on the first quiz (0 - 10): ");
    		int quiz1 = keyboard.nextInt();
    
    		System.out.println("Enter the student's score on the second quiz (0 - 10): ");
    		int quiz2 = keyboard.nextInt();
    
    		System.out.println("Enter the student's score on the third quiz (0 - 10): ");
    		int quiz3 = keyboard.nextInt();
    
    		System.out.println("Enter the student's score on the midterm (0 - 100): ");
    		int midterm = keyboard.nextInt();
    
    		System.out.println("Enter the student's score on the final (0 - 100): " );
    		int finalExam = keyboard.nextInt();
    
    		StudentRecord studentB =
    			new StudentRecord(quiz1, quiz2, quiz3, midterm, finalExam);
    		
    		System.out.println("Student A's record: " + studentA); 
    		System.out.println("Student B's record: " + studentB); 
    
    		if (studentA.equals(studentB))
    			System.out.println("Student A has a same record as Student B");
    		else	
    			System.out.println("Student A has a different record than Student B");
    
    	}
    	
    /***********************************************************************************
     * This method calculates the numericScore based on the scores of the quizzes and exams.
     * The score are also weighted.
    ***********************************************************************************/
    	public void computeNumericScore() 
    	{
    		double quizTotal = 0, weightedQuiz = 0, weightedMidterm = 0, weightedFinalExam = 0;
    		double numeriScore = 0;
    		
    		quizTotal = ((this.quiz1 + this.quiz2 + this.quiz3)/30) * 100;
    		weightedQuiz = (quizTotal * .25);
    		weightedMidterm = (this.midterm * .35);
    		weightedFinalExam = (this.finalExam * .45);
    		
    		this.numericScore = (weightedQuiz + weightedMidterm + weightedFinalExam);
    				
    	}
    
    /***********************************************************************************
     * This method calculates the letterGrade based on the numberScore.
     **********************************************************************************/
    	public void computeLetterGrade()
    	{
    		computeNumericScore();
    		
    		if (this.numericScore >= 90)
    		{
    			letterGrade = 'A';
    		}
    		else if ((this.numericScore >= 80) && (this.numericScore < 90))
    		{
    			letterGrade = 'B';
    		}
    		else if ((this.numericScore >= 70) && (this.numericScore < 80))
    		{
    			letterGrade = 'C';
    		}
    		else if ((this.numericScore >= 60) && (this.numericScore < 70))
    		{
    			letterGrade = 'D';
    		}
    		else if (this.numericScore < 60)
    		{
    			letterGrade = 'F';
    		}
    
    	}
    
    /***********************************************************************************
     * This method compares two StudentRecord objects. It will return true only if two 
     * objects have same score for each of the quizzes and exams.
     **********************************************************************************/
    	public boolean equals(StudentRecord studentB) 
    	{
    		return (this.quiz1 == studentB.quiz1) && (this.quiz2 == studentB.quiz2) 
    				&& (this.quiz3 == studentB.quiz3) && (this.midterm == studentB.midterm) 
    				&& (this.finalExam == studentB.finalExam);
    	}
    
    /***********************************************************************************
     * This method returns a string representation of the data in the calling object.
     **********************************************************************************/ 
    	public String toString() 
    	{
    	    computeNumericScore();
    	    computeLetterGrade();
    	    
    	    return "Quiz 1: " + this.quiz1 + ", Quiz 2: " + this.quiz2 + ", Quiz3: " + 
    	           this.quiz3 + ", Midterm: " + this.midterm + ", Final: " + this.finalExam + ".  " 
    	           + "Overall Grade: " + this.numericScore + ", Letter Grade: " + this.letterGrade;
    	}
    
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default Re: non-static variable cannot be referenced from a static context

    I bet the command line gave more information than just that one line. Paste the exact compiler output. Nobody will be bothered to recompile to find out what's going on.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    tngo is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: non-static variable cannot be referenced from a static context

    This is the output that I get from command line.

    StudentRecord.java:103: non-static variable quiz1 cannot be referenced from a static context
    StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3,midterm, finalExam);
    ^
    StudentRecord.java:103: non-static variable quiz2 cannot be referenced from a static context
    StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3,midterm, finalExam);
    ^
    StudentRecord.java:103: non-static variable quiz3 cannot be referenced from a static context
    StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3,midterm, finalExam);
    ^
    StudentRecord.java:103: non-static variable midterm cannot be referenced from a static context
    StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3,midterm, finalExam);
    ^
    StudentRecord.java:103: non-static variable finalExam cannot be referenced from a static context
    StudentRecord studentA = new StudentRecord(quiz1, quiz2, quiz3,midterm, finalExam);
    ^
    5 errors

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: non-static variable cannot be referenced from a static context

    You are trying to instantiate your StudentRecord instances using the attributes of StudentRecord.
    I suspect you are supposed to get those values (quiz1, 2, 3 etc) from somewhere else.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. Replies: 7
    Last Post: 12-07-2012, 01:00 PM
  2. Replies: 5
    Last Post: 12-01-2010, 06:43 AM
  3. Replies: 5
    Last Post: 10-15-2010, 04:21 AM
  4. Replies: 3
    Last Post: 02-11-2010, 09:59 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
  •