Issue
I have a problem: I have to create a project for my studies. In the requirements I have a information that the whole project should be created in two files A.java and B.java.
The problem of project is complicated so I have created some helpfull class called e.x. MyClass. I put this class inside A.java - I cannot use another file.
Right now my A.java looks like that:
public class A {
// some logic
}
final class MyClass implements Comparable<MyClass> {
//some logic
}
In the B.java I am using objects of MyClass and everything is working correctly.
The problem is with the second requirement of project. The automatic compilation - after I will send my project to my university - starts the proccess like that:
javac –Xlint B.java A.java
Compilation is successfull but I have some warnings:
B.java:17: warning: auxiliary class MyClass in A.java should not be accessed from outside its own source file
private MyClass variable;
The third requirement says that if you have any warnings or errors, your project will not be assessed. So I will fail my project if the warnings appears.
I know this is very stupid to store two classes in one file but this is unversity - here everything is stupid...
So - is there any solution to turn off warnings during compilation with javac? I tried this:
@SuppressWarnings("all")
final class MyClass implements Comparable<MyClass> {
but it doesn`t work - I still get the warnings...
Any ideas? :)
Solution
Put MyClass
inside A
:
public class A {
// some logic
static final class MyClass implements Comparable<MyClass> {
//some logic
}
}
and refer to it as A.MyClass
from the other file.
Or import it (assuming A
isn't in the default package), and refer to it as MyClass
.
Or defining it in its own file would be the obvious option; but you say you can't do that.
Answered By - Andy Turner
Answer Checked By - Terry (JavaFixing Volunteer)