Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9073661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:28:03+00:00 2026-06-16T18:28:03+00:00

in my program there are three class Student,School and TestStudent. I have declared students

  • 0

in my program there are three class Student,School and TestStudent. I have declared students state inside student class and also there are methods for getting students subject,i have created an array list of type student in School class,but when i try to access student’s method in school i get error newStudent type can not be resolved.Here are my codes.

public class Student {
        String name;
    String subject;
    int age;
    Student(String name,String subject,int age){
    this.name = name;
    this.subject = subject;
    this.age = age;
}
public void setName(String name){
    this.name = name;
}
public String getName(){
    return this.name;
}
public String getSubject(){
    return this.subject;
}
public int getAge(){
    return this.age;
}

}

public class School {
public ArrayList <Student> students = new ArrayList <Student>(); 
public void addStudent(String name,String subject,int age){
    Student newStudent = new Student(name,subject,age);
    students.add(newStudent);
}
public void showSubject(String student){
     newStudent.getSubject();


}

} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:28:04+00:00Added an answer on June 16, 2026 at 6:28 pm
    newStudent.getSubject();
    

    This is not what you want. Because you haven’t retrieved that student yet from the ArrayList.

    You would need to iterate over the ArrayList, and see which student have the name as passed in parameter.
    So, just use a for-each loop to iterate over your ArrayList, and return appropriate Student.

    So, your method should look like: –

    public void showSubject(String student){
         for (Student student: students) {
            if (student.getName().equals(student)) {
                System.out.println(student.getSubject());
            }
         }
    }
    

    Note that, using a Map here would be a better idea as explained by @Peter in his answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question. There is application class in my program. It is inherited
There are many methods for representing structure of a program (like UML class diagrams
I have a text editor like program which is a QMainWindow inherited class. There,
I'm writing an expense tracking program. At the moment I have three class Lineitem
I have two classes, a Teacher class and a Student class. In my program
I'm a student currently working on a powerball program for class. I have made
In my program, there are two class, say, category and product, with each object
I'm writing a simple program. There is only one class in it. There is
I have a program, and in that program there is some variables (username and
I have a program where there is a master/slave setup, and I have some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.