0

My task is to display the value variable grade for objects S1 and S2.

my code:

public class StudentTester{
public static void main(String[] args){
       student S1 = new student();
       student S2 = new student();
       S1.setGrade(12);
       System.out.println("Student one: " +S1+", Student two: "+S2);
}

public static class student {

    private String name;
    private int age;
    private int grade;
    private double average;
    private boolean disability;
    
    private void printStudentInfo(){
        //Data Encapsulation is methods of the public interface provide access to private data, while hiding implementation. 
        System.out.println("Name: "+name+",Age: "+age+",Grade: "+grade+",Average: "+average +" Disability: "+disability);
    } 
    
    public void setGrade(int newGrade){
        grade=newGrade;
    }
    
    public int getGrade(){
        return grade;
    }
 }
}

When I print it get an an output like this:

Student one: StudentTester$student@ba4d54, Student two: StudentTester$student@12bc6874

spider
  • 15
  • 3
  • 3
    Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – OH GOD SPIDERS Jan 12 '21 at 10:21
  • implement toString method for Student class – A.RAZIK Jan 12 '21 at 10:23

1 Answers1

0

You should access the fields using the object

public static void main(String[] args){
       student S1 = new student();
       student S2 = new student();
       S1.setGrade(12);
       System.out.println("Student one: " +S1.getGrade()+", Student two: "+S2.getGrade());
}
Zach_Mose
  • 357
  • 3
  • 7