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? :)
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.
Related
I have two packages. The first one contains an empty interface and a class implementing it in a single file ("IThing" and "Thing"). The second one contains another Interface ("IThingUser") which has a function returning an object of the type "Thing".
When both files are part of the same package everything works fine, but if they are in two separate packages the one in package2 cannot access the class defined in the first package.
Package1 contains the following file :
package project.package1;
public interface IThing {
}
final class Thing implements IThing {
private int thingField;
public int thingFieldGetter(){
return thingField;
}
}
And package2 has :
package project.package2;
import project.package1.IThing;
public interface IThingUser {
public IThing someFunction(); // Works fine
public Thing anotherFunction();
// "Thing" is not recognized when the two files are in separate packages.
}
Why does this happen ? Is there a way to fix this issue while keeping this architecture ?
PS : I know the structure of this does not make much sense but I did not code package1 and I have to use it as-is.
The problem is that project.package1.Thing is not visible outside the package project.package1, but public classes must be defined in their own files.
Class Thing has package-private visibility. You wouldn't be able to access it outside project.package1 package until it will be implemented as
public final class Thing implements IThing
I'm working in Java and have come across an incredibly odd error. I have a very basic class as follows:
public class ClassA{
private static Logger log = Logger.getLogger(ClassA.class.getName());
private boolean trace;
public ClassA(){
trace = log.isTraceEnabled();
}
public void doSomething(){
//does stuff
}
}
I can use this class just fine within my current project. However, when I build, package, and install to my local repo (using Maven, no remote artifact repo set up), other projects cannot properly use this class because they cannot instantiate it. When I try anything like:
ClassA classA = new ClassA();
I get the following compilation error:
ClassA() has private access in [package].ClassA
I've decompiled the .jar in my local repo to ensure the constructor is present and is public - it is. I've also used the -U flag to force updates and the compilation continues to fail. What could be causing this error?
Maybe you have some other ClassA.class file somewhere in the classpath. Check all the jars used by the project that cannot call the constructor: one of them should contain an old version of your class.
My only thought is that you have a problem with your package. Make sure to define the package at the top of the source file for classA using the package keyword. When you call it ensure that the file is in include list with the include keyword. You could be running into the error because ClassA exists in some default package and that is what you are actually calling instead of calling your locally made ClassA class. The code you posted looks fine and you have already double checked to ensure the changes have taken effect in your repository.
//for those with Kotlin-Java mixed projects:
If the said file (With constructor) is in Kotlin and is being used in Java:
Instead of A a = new A(); //which causes the said error
Use A.INSTANCE. …
I have this error, where write "private", instead "public" for class constructor;
When decompiling a specific jar using java decompiler (http://java.decompiler.free.fr/) I got some strange code I cannot identify what is. can someone help me? the code is something like:
Foo.access$004(Foo.this);
or this
Bar.access$006(Bar.this);
or else
Baz.access$102(Baz.this, true)
What are these methods access$004, access$006 and access$102?
Synthetic methods like this get created to support acessing private methods of inner classes. Since inner classes were not part of the initial jvm version, the access modifiers could not really handle this case. The solution was to create additional package-visible methods that delegate to the private implementation.
public class Example {
private static class Inner {
private void innerMethod() { ... }
}
public void test() {
Inner inner = ...
inner.innerMethod():
}
}
The compile would create a new method of the Inner class like this:
static void access$000(Inner inner) {
inner.innerMethod();
}
And replace the call in the test method like this:
Inner.access$000(inner);
The static access$000 is package visible and so accessible from the outer class, and being inside the same Inner class it can delegate to the private innerMethod.
These are auto-generated methods which are created by the compiler in some cases (for example when accessing private fields of another class directly, e.g., in case of nested classes).
See also What is the meaning of "static synthetic"? and Synthetic Class in Java.
If you get the relevant .class file (run jar through unzip), and run the .class file through JAD
JAD MyClass.class
then you may find that the output JAD file has decompiled that particular line in a more meaningful way, e.g.
Baz.access$102(Baz.this, true)
shows up in the JAD output as simply
myMemberVaiable = true
where myMemberVaiable is a member of class Baz that you will recognise.
I have a number of .java classes compiled with Eclipse and over in the /bin directory I see that not only do I have various .class files corresponding to my Java classes but also a few with a dollar sign in the file name.
Example: I have a class called RangeFinder and in the /bin I see a RangeFinder.class and also a RangeFinder$1.class.
What is the significance of the latter?
(I am on Ubuntu and I am using Eclipse EE Indigo.)
These are anonymous inner classes in bytecode form. The compiler gives them numerical names starting with 1 (it is not allowed in Java to have a class name starting with a number, but it is possible in the bytecode, so the compiler does it to avoid name clashes, I guess). Normal (named) inner classes are named like OuterType$InnerType.class.
Each .java source file can contain definitions for multiple Java classes. However, every .class file can only contain the bytecode for a single class.
So, if you have a file Foo.java that looks like this:
public class Foo {
public class Inner {
…
}
public void method() {
widget.addListener(new Listener() {
public void listen() {…}
}
}
}
class Bar {
…
}
It will compile into the following files:
Foo.class
Foo$Inner.class
Foo$1.class
Bar.class
Anonymous classes are assigned numbers as "names", I believe in the order in which they're found in the enclosing class.
I am running into this problem when trying to recompile a single class inside a package.
Now this class uses global types and some of these global types reference it. So taking it out of the package really isn't an option.
So when I try to compile it with javac alone, I get invalid symbol errors and netbeans shows it is trying to compile things like classespackage.globaltype. Basically it is searching for the global classes inside of the package. Is there anyway to stop it from doing that?
Here is the code:
Global
public class Global {
example.Main main;
public Global(example.Main m) {
main = m;
}
}
example.Main
package example;
public class Main {
public static void main(String[] args) {
Global g = new Global(new Main()); // COMPILE ERROR
}
}
I get invalid symbol errors
You probably meant "Cannot find symbol" errors? This can be caused by anything. Imported class which is not in the compiletime classpath, methods which does not exist, variables which are out of the scope. You really need to post the compilation errors to get more detailed answers.
At least, this much sounds like that you didn't specify the dependencies (the imported classes) in the compiletime classpath using the -cp or -classpath argument.
Is there anyway to stop it from doing that?
By listening to those errors and taking actions accordingly.
Update as per the posted code example: the cause of the problem is that classes in the default package (i.e. classes without a package declaration) are invisible to classes inside a concrete package (i.e. classes with a package declaration). You need to put Global in a package. Then it's visible (importable) to classes inside a package.
If you reference the global types in a class package, you'll have to include them in your classpath. When you do javac, make sure you include the global types class in your classpath (with the -cp option.)