Two java files. Getting IllegalAccessError when running class with main method trying to access a method from the other file

前端 未结 3 359
野的像风
野的像风 2020-12-22 08:12

Learning Java. I have two files, each containing one java class. When I run the file with the main method, I get the following error:

Exception in threa

3条回答
  •  离开以前
    2020-12-22 08:56

    Quick possible fix, try making the classes and methods "Public".

    Under normal circumstances this shouldn't be necessary but you may be hitting a specific case where it might be (It's an edge case so I don't know if it's a problem or not off the top of my head):

    Java is really uncomfortable with stuff being in the "Default" package--code from other packages can't access objects in the default package (Meaning no package statement). Although this shouldn't cause problems in your case, maybe your "package" level security settings are also not working in the default package.

    If this is actually the problem, the two fixes would be to make the packages and methods public (as I said above) or move both classes into a package.

提交回复
热议问题