Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By jim829

Thread: Cant access a variable form another class...

  1. #1
    hawk-dc is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Cant access a variable form another class...

    Hi! can smb help me why I cant access a variable from another class, in particular why in the main class cant i assign " quiz1 = getQuizOne();" ? It constantly giving me error.
    Java Code:
     import java.util.Scanner;
    public class Grade {
    	
    	private int quiz1, quiz2, midtermExam, finalExam = 0;
    	
    	public static void main(String[] args)
    	{
    		Student John = new Student();
    		John.StudentData();
    		
    		
    	}
    	public void courseAverage()
    	{
    		
    		quiz1 = getQuizOne();
    		quiz2 = getQuizTwo();
    		
    		
    		
    		
    		
    		
    	}
    	
    }
    Here is another class:
    Java Code:
    import java.util.Scanner;
    public class Student {
    	
    	private String name;
    	private int quiz_One;
    	private int quiz_Two;
    	private int midtermExam;
    	private int finalExam;
    	
    	Scanner kbd = new Scanner(System.in);
    	public void StudentData()
    	{
    		System.out.println("Hello! Please enter your information and grades: ");
    		System.out.println("Name: ");
    		name = kbd.nextLine();
    		System.out.println("Enter your grades for the Quiz 1 and 2: ");
    		quiz_One = kbd.nextInt();
    		quiz_Two = kbd.nextInt();
    		System.out.println("The last step, please enter the grades for the Midterm and Final: ");
    		midtermExam = kbd.nextInt();
    		finalExam = kbd.nextInt();
    	}
    	public int getQuizOne()
    	{
    		return quiz_One;
    	}
    	public int getQuizTwo()
    	{
    		return quiz_Two;
    	}
    	public int getMidterm()
    	{
    		return midtermExam;
    	}
    	public int getFinal()
    	{
    		return finalExam;
    	}
    	
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Cant access a variable form another class...

    You need to create an instance of Student and use that as a reference to the desired method. Just like you did with John.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    hawk-dc is offline Member
    Join Date
    Nov 2013
    Posts
    22
    Rep Power
    0

    Default Re: Cant access a variable form another class...

    Thank you for the reply, but why do I need to create an instance of Student if I already created one ( john)?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: Cant access a variable form another class...

    Well, then use John. The point is you need to use a reference of Student to access any of class Student's instance methods.

    Regards,
    Jim
    hawk-dc likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 1
    Last Post: 10-29-2013, 11:04 PM
  2. How to access a variable in one class from another?
    By Snake006 in forum New To Java
    Replies: 45
    Last Post: 07-19-2013, 08:29 PM
  3. access parent form public member from child form
    By .paul. in forum New To Java
    Replies: 1
    Last Post: 07-21-2012, 02:12 AM
  4. Access HTML form in Java
    By K-ML in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2012, 09:48 AM
  5. How to Access and Set variable in parent class
    By java_newbie88 in forum New To Java
    Replies: 6
    Last Post: 01-20-2011, 02:48 AM

Tags for this Thread

Posting Permissions

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