简体   繁体   中英

Classes in same package

I love the Intellij IDEA but i have been stacked on one little problem with Java imports. So for example there is a package with name "example" and two different classes in it: A.java and B.java.

And i wanna get access to class "A" from class "B" without imports. Like this:

class A:

package example;

public class A{ ... some stuff here ...}

class B:

package example;

public class B{
    public static void main(String[] args){
        A myVar = new A();
    }
}

This code may not work, but it's doesn't matter. Trouble just with IDE and with its mechanism of importing classes.

So, problem is that i can't see A class from B. Idea says 'Cant resolve symbol' but i actually know that class A exists in package. Next strange is that complier works fine and there are no exceptions. Just IDEA can't see the class in the same package.

Does anybody has any ideas?

If they are in the same package, you can access A in class B without import:

package example;

public class B{
    public static void main(String[] args){
        A myA = new A();
    }
}

Maybe this will help you, or somebody else using IntelliJ that is getting a "Cannot resolve symbol" error but can still compile their code.

Lets say you have the two files that buymypies wrote up, the standard Java convention is that the two files would exist in an Example directory in your source code area, like /myprojectpath/src/Example . But it is technically not a requirement to reflect the package structure in the source directory structure, just a best practice sort of thing.

So, if you don't mimic the package structure, and the two files are just in /myprojectpath/src , IntelliJ will give you the "Cannot resolve symbol" error because it expects the source code structure to reflect the package structure, but it will compile okay.

I'm not sure if this is your problem, but I do use IntelliJ and have seen this, so it's something to look at.

I have the same problem as this: 2 classes in the same package, yet when one tries to call the other, Intellij underlines it in red and says Cannot resolve symbol 'Classname', eg Cannot resolve symbol 'LocalPreferencesStore'.

It then wants to add the fully qualified name in situ - so it clearly knows the path - so why can't it just access the class?

The module still compiles and runs, so it's just the IDE behaving oddly - and all that red is very distracting, since it isn't actually an error, it's just IDEA throwing a weird wobbly.

This is also recent. Two weeks ago I wasn't having this problem at all, and now suddenly it's started up. Of course, it could go away again on its own soon, but it's really annoying.

Same issue and I just fixed it.

I don't know your folder structure. However, if your package example was added manually.That's the problem. The package should be the same as your folder structure,which means if you want your class file to be stored in package example ,you must store you java file in the src's subfolder named example.

Please ensure that both A.java and B.java are in the same package. Since Both are in same package, no need to specify package example;

Class A:

public class A{ ... some stuff here ...}

Class B:

public class B{
    public static void main(String[] args){
        A myVar = new A();
    }
}

You need to learn the basics about Java i think.

Here is a basic example of what i think you are trying:

package Example;

public class A
{
    String myVar;

    public String getMyVar()
    {
        return myVar;
    }

    public void setMyVar(String myVar)
    {
        this.myVar = myVar;
    }
}

You need to create an instance of A.

package Example;

public class B
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        A myA = new A();

        myA.setMyVar("Hello World!");

        System.out.println(myA.getMyVar);
    }

}

Look up java 'getters' and 'setters'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM