-1

How to remove error and make method work properly?

Error: cannot find symbol test.say();

symbol: method say()

class Untitled {
  public void main(String[] args) {
    Student test = new Student();
    test.say();
  }
  public class Student {
    public String name = "John";
    public String studentindex = "23";
    public String group = "11c";    
  }
  public void say () {
    Student stud = new Student();
    System.out.println (stud.name);
    System.out.println (stud.studentindex);
    System.out.println (stud.group);
  }
}

Thank you

dave
  • 11,641
  • 5
  • 47
  • 65
Mark Ladyshev
  • 41
  • 1
  • 3

1 Answers1

1

The method say() is actually defined on your Untitled class, not your Student class hence the compiler warning.

You probably want to change your main() method to something like:

public void main(String[] args) {
  Untitled test = new Untitled();
  test.say();
}

Alternatively, you could move say() "inside" Student thus:

public class Student {
  public String name = "John";
  public String studentindex = "23";
  public String group = "11c";    
  public void say () {
    Student stud = new Student();
    System.out.println(stud.name);
    System.out.println(stud.studentindex);
    System.out.println(stud.group);
  }
}

If you take this route, then I'd suggest updating say() to print the current values, rather than creating a new Student. Something like:

public class Student {
  public String name = "John";
  public String studentindex = "23";
  public String group = "11c";    
  public void say () {
    System.out.println(this.name);
    System.out.println(this.studentindex);
    System.out.println(this.group);
  }
}
dave
  • 11,641
  • 5
  • 47
  • 65