0

My task is : Call the ‘setGrade’method to assign to the grade variable of S1 the value of 11

the code is:

public 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;
    }

    //this part was given from the question 

    //from here
    public class StudentTester{
       public static void main(String[] args){
           student S1 = new student();
           student S2 = new student();
           //to here

           S1.setGrade(12);
    
       }
    
    }
}

Every time I run my program it gives me an error

modifier 'static' is only allowed in constant variable declarations

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
spider
  • 15
  • 3

1 Answers1

0

The tester class should contain the main method along with other static classes and methods.

public class StudentTester{
    public static void main(String[] args){
        student S1 = new student();
        student S2 = new student();
        S1.setGrade(12);
    }
 
    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;
        }
     }
  }

Also check, Static Classes In Java

XO56
  • 332
  • 1
  • 12