非法修饰符,只允许 final 吗?

Illegal modifier, only final is permitted?

所以我正在尝试创建一个私有静态方法,它将使用文本文件中的数据打印数组的内容。

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class hw2redo 
{
    public static void main(String args[])
    {
         //Scan file for data
         GeometricObject g = null;
         BufferedReader file = new BufferedReader(new FileReader("file.txt"));
            Scanner diskScanner = new Scanner(file);
            //Create dynamic array list
            List<GeometricObject> list = new ArrayList<GeometricObject>();
            //Scan data and add data to list
            while(diskScanner.hasNext())
            {
                String geolist = diskScanner.nextLine();
                g = recreateObject(geolist);

                list.add(g);

            }

            private static GeometricObject recreateObject(String data)
            {
                System.out.println(data);
            }
    }

}

但是,我的 recreateObject 方法出现错误,显示 "The method recreateObject(String) is undefined for the type hw2redo." 任何关于我做错事情的帮助或提示都将不胜感激。谢谢!旁注:我只是在方法中随意添加了一些内容,以查看错误是否是因为它是空的。我只是想知道如何在不出错的情况下调用私有方法。

您的 recreateObject 方法定义在 内部 main 方法中。将它移到 main:

的主体之外
public class hw2redo 
{
    public static void main(String args[])
    {
         //Scan file for data
         GeometricObject g = null;
         BufferedReader file = new BufferedReader(new FileReader("file.txt"));
            Scanner diskScanner = new Scanner(file);
            //Create dynamic array list
            List<GeometricObject> list = new ArrayList<GeometricObject>();
            //Scan data and add data to list
            while(diskScanner.hasNext())
            {
                String geolist = diskScanner.nextLine();
                g = recreateObject(geolist);

                list.add(g);

            }


    }

        private static GeometricObject recreateObject(String data)
        {
            System.out.println(data);
        }

}

(而且,显然,让它成为 return 东西。)