1

I have 2 .java files I'm working with, one that is executable and the other one is a custom class.

I understand that during compilation, the custom class will be automatically imported since it's in the same folder on my machine. However, I'm using NetBeans and if I don't 'pretend' to import it in NetBeans, it would always show up with red lines indicating errors when I type the class object. Is there any way to 'import' the class .java file into NetBeans?

I'm a noob in java so I'm not sure how to questions like this one, sorry if my use of vocabulary isn't appropriate. Thank you very much!

2
  • 2
    This doesn't make sense. If the two classes are in the same package, there is no need to import anything, pretend or otherwise. Commented Dec 2, 2012 at 3:42
  • @HovercraftFullOfEels - That's not quite right--consider nested classes. Commented Dec 2, 2012 at 20:59

1 Answer 1

1

If you have the two .class files in the same location, Java will always find the second one. The same location might be, for instance, a folder on your computer, a directory on a web server, or inside a .jar file. As long as Java can find one of them, it will find the other.

There are many tutorials on the web about Java packages and the Java classpath. These concepts are important to understand as you start to write larger programs. A good place to start is the Java Tutorial on Packages.

Sign up to request clarification or add additional context in comments.

6 Comments

is there any way I can make NetBeans stop thinking i made an error every time i use the object in the class i'm referring to? So that i can code and make sure of any mistakes before compiling? :P
@user1869735 - I'm not sure why NetBeans would complain. Did you create the files as part of a project in NetBeans, or are you just editing the files using NetBeans? If the latter, that would explain the problem; just create a Java project in NetBeans, bring in your files (or recreate them within NetBeans), and all should be well.
After I embedded the class into my executable java file, only when i use the object in the main class is it recognized and not red underlined. When I tried to use it in my other methods, NetBeans still complains :(
@Ponnnnn - What do you mean by "embedded the class into my executable java file"? Java files are not executable. Do you mean that the class is declared as a nested class inside the class that contains public static void Main(String[])? If so, then in other files, you need to refer to it by its qualified name, since it is not a top-level class. Another solution would be to move the class to its own file.
since i'm using the javac command in command prompt to compile my executable java file. I put the other java file with the class/object i'm using into the same folder. How would I be able to tell my compiler that i'm using the object from the other java file in the same folder?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.