I have a Java project built using IDEA and Maven. To make this question simple:
suppose part of the structure of the project is
src -> main -> java
In java folder there is a package called PAK, for example, which contains class A. Also there is class B in java folder without package.
The problem is when I'm trying next code
package PAK;
public class A {
private B variable;
}
compiler can't see class B but class B is public.
You need to import the class B, because it's not in the same package with A
package PAK;
import B;
public class A {
private B variable;
}
If classes are in the same package, you don't need to import them.
Related
I have 2 folders in my project, A and B. A contains the classes I want access to but the classes in there are part of their own package. In folder B, I have a similar setup but I need to call the classes in folder A. I tried regular import statements but that didn't work. The 2 packages are in completely separate folders, each with their own settings. They're 2 seperate springboot applications and I'm trying to get them to work with each other. How would I do this?
This is what I'm talking about:
package A;
import classes;
public class Controller{
//methods I need
}
What I tried was:
package B;
import classes from B;
import packageA.classesfromA;
public class ControllerB{
//methods
}
Screenshots:import error from B,
the 2 folders that contain what I need, B is partyinthebackend, cs320EthicsPlayer is A
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've got structure of catalogs:
/home/etc./studies/JAVA/pack/Print.java
/home/etc./studies/JAVA/Lab2/zad1/pkg/A.java
/home/etc./studies/JAVA/Lab2/zad1/B.java
A is a class in package "pkg"
Print is a class in package "pack"
B imports the packages "pkg" and "pack".
When I tried to compile B.java, I get an error:
B.java:4: error: cannot access A
public class B extends A{
^
bad class file: /home/etc./studies/java/A.class
class file contains wrong class: pkg.A
Please remove or make sure it appears in the correct subdirectory of
the classpath.
Is it possible to include that packages without reorganization structure of files ?
It's clearly saying that:
class file contains wrong class name: pkg.A
that means probably you declared class name as pkg. An instead of A.
if you declare the package names as correct like this
home/etc/studies/JAVA/Lab2/zad1
home/etc/studies/JAVA/Lab2/zad1/pkg,
/home/etc/studies/JAVA/Lab2/zad1/B.java
You won't get the compilation error.
package home.etc.studies.JAVA.Lab2.zad1;
import home.etc.studies.JAVA.Lab2.zad1.pkg.A;
public class B extends A {
'enter code here`
}
You cannotreference Classes in a default package. Put every class into a package.
I have built a simple project called LibTest that has one class with the following code:
public class MainTest
{
public static tclass l;
}
In secondary simple project I have the defined class tclass:
public class tclass
{
int i;
}
Then I export tclass to a JAR file. At LibTest->Properties->BuildPath I click on AddExternalJar and select tclass.jar ( I also tried checking the JAR at Order and Export) but I still get an error at MainTest "tclass cannot be resolved to a type".
I don't see what is missing.
Thanks
Simon
After including your jar file, you also need to import your class using its full path in your MainTest class. e.g:
import com.package.tclass;
company xyz created a package
com.xyz.utils.
There are two classes declared in two separate files. They have some variables as package private. so that a variable X in class A can be used in class B of the same package.
package com.xyz.utils;
public class A{
int a=10;
}
package com.xyz.utils;
public class B{
int b = (new A()).a;
}
Those two files are compiled into a jar and sent to customer.
The customer add the jar to the project he is building and he writes code like below
package com.xyz.utils;
public class customer_class
{
int Y = (new A()).a;
}
Is that above code is correct?
My quetsion is. how can we make variables which are declared as package private to be not visible to others when they use the package.
The answer is "no" - you can't stop them from doing that.
It can not be done in general. I think, you can seal the package 'com.xyz.utils' in in the jar manifest, to tell the user that: do not define their classes in the sealed package as a best practice. But you can not restrict the user of your library from doing it.