'Netbeans doesnt recognize class in the same package
i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure. The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol". If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class. If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
Solution 1:[1]
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
Solution 2:[2]
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
Solution 3:[3]
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Enrique |
Solution 3 | S.H.Kashani |