This question already has answers here:
What is the difference between a member variable and a local variable?
                            
                                (7个答案)
                            
                    
                2年前关闭。
        

    

我已经看过所有其他有关从main方法访问对象实例的其他stackoverflow问题,但我尝试过的任何方法都没有起作用,但我仍然继续收到以下错误消息

java.59: error: cannot find symbol
        System.out.println(student2.getStudentId());


这是我整理的主要方法:

import java.util.Scanner;
import java.io.*;
import java.util.Arrays;

public class Testing {

  public static void main (String [] args) throws IOException {


  final String INPUT_FILE  = "c:\\Users\\XXXXXX\\Documents\\Input01.txt";
  Scanner br = new Scanner(new File(INPUT_FILE));

// Test 2 - Test first and second line of information from input file

  String[] strArray;
  while (br.hasNext()) {
  strArray = br.nextLine().split(",");
     if(strArray[0].equals("STUDENT")) {
        processStudentData(strArray);
        System.out.println(student2.getStudentId());
     }
     else
     if(strArray[0].equals("GRADE ITEM")) {
        processGradeItemData(strArray);
     }

  }

  } //end main

// ***************************************************************

// Uses string array from input file to create new student object
public static void processStudentData(String[] a) {

  System.out.println("Running Test 2a:");
  if (a[1].equals("ADD")) {
     Student student2 = new Student(a[2], a[3], a[4], a[5]);
  }

} // End processStudentData

} //end Testing


这是我的学生班级精简版:

public class Student {

private String studentId;           // Unique ID for each Student
private String studentFirstName;    // Student's legal first name
private String studentLastName;     // Student's legal last name
private String studentEmail;        // Student's school email address

//************************************************************************

  Student() {

  } // end Student

//************************************************************************

  public Student(String studentId, String studentFirstName, String studentLastName,
                  String studentEmail) {

     if (studentId.equals("")) {
        throw new IllegalArgumentException("Student ID cannot be blank.");
     }
     if (studentFirstName.equals("")) {
        throw new IllegalArgumentException("Student first name cannot be blank.");
     }
     if (studentLastName.equals("")) {
        throw new IllegalArgumentException("Student last name cannot be blank.");
     }
     if (studentEmail.equals("")) {
        throw new IllegalArgumentException("Student email cannot be blank.");
     }
     if (!studentEmail.contains("@")) {
        throw new IllegalArgumentException("Student email must have '@'.");
     }

     this.studentId = studentId;
     this.studentFirstName = studentFirstName;
     this.studentLastName = studentLastName;
     this.studentEmail = studentEmail;

  } // end Student

//************************************************************************

public String getStudentId() {

  return studentId;

} // end getStudentId


我需要能够使用我的get方法从main方法中打印出学生信息,而仍然通过processStudentData方法实例化student2对象。我不想将我的get方法更改为static,因为将有多个实例。

任何帮助将不胜感激。

更新:我添加了对processStudentData方法的返回,并且仍然收到与以前相同的错误(主要方法未更改,processStudentData方法已在下面更新):

public static Student processStudentData(String[] a){
  System.out.println("Running Test 2a:");
  if (a[1].equals("ADD")) {
     Student student2 = new Student(a[2], a[3], a[4], a[5]);
     return student2;
  }
  return null;
}

最佳答案

由于student2被定义为ifprocessStudentData语句内的局部变量,因此只能在定义该上下文的上下文中使用。

// Uses string array from input file to create new student object
public static void processStudentData(String[] a) {

  System.out.println("Running Test 2a:");
  if (a[1].equals("ADD")) {
     Student student2 = new Student(a[2], a[3], a[4], a[5]);
  }

} // End processStudentData


“ A”解决方案是将结果返回给调用方。

// Uses string array from input file to create new student object
public static Student processStudentData(String[] a) {

  System.out.println("Running Test 2a:");
  if (a[1].equals("ADD")) {
     return new Student(a[2], a[3], a[4], a[5]);
  }

  return null;

} // End processStudentData


然后您可以使用类似...

Student student = processStudentData(strArray);
if (student != null) {
    System.out.println(student.getStudentId());
}


您可能需要仔细看看:


Language Basics/Variables
Returning a Value from a Method


更多细节

10-07 16:20
查看更多