0

I have two java classes (.java files). I want to be able to create new instances of an object in one class that were defined in the other class, without referencing the class name each time. In C# there are #using and includes, but I am only able to use import <pre-compiled code> in java. Is there a way to do this:

import TestClass;

and then simply call a function inside it without using

TestClass.TestFunction()

every time? I simply need to have all of my functions and objects to be separate from my Main class.

Bit Fracture
  • 651
  • 1
  • 9
  • 24

2 Answers2

5

Assuming TestFunction is a static method in TestClass, you can use a static import:

import static TestClass.TestFunction;
// or
import static TestClass.*;

and then call it without using the class qualifier:

TestFunction(...);

Note this can lead to confusing/hard-to-read code – use static imports sparingly.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • I get a "Cannot find symbol" error on the class name, but it certainly does exist. This is why I am confused. I have tried so many different things and still get errors. – Bit Fracture Feb 19 '13 at 04:13
  • You need to import the fully-qualified class name. You didn't give one in your question so I didn't put it into my answer. As an example, however, you could statically import all of the functions and constants from [`Math`](http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html) like so: `import static java.lang.Math.*;` – Matt Ball Feb 19 '13 at 04:16
  • I understand, but this is a class I have created. It is a .java file, and it is in the same directory as my Main.java project file. No prefixes I add seem to fix it. – Bit Fracture Feb 19 '13 at 04:18
  • What package are these two classes in? I repeat: you need to import the **fully-qualified** class name. – Matt Ball Feb 19 '13 at 04:21
  • I can't figure out the fully qualified class name, I literally switched from C# to java about 6 hours ago. These are both in `MYPROJECT -> Source Packages -> ` according to net beans. – Bit Fracture Feb 19 '13 at 04:23
  • 2
    Ah - there's your problem. **Don't use the default package.** [Classes in the default package cannot be imported.](http://stackoverflow.com/q/3835279/139010) – Matt Ball Feb 19 '13 at 04:25
  • Got ya! I moved it to its own package in the project called TESTPACKAGE. Now how would I reference it? Doesn't seem to find the class, but it sees the package. – Bit Fracture Feb 19 '13 at 04:27
  • 1
    `import static TESTPACKAGE.TestClass.TestFunction;` – Matt Ball Feb 19 '13 at 04:34
  • There is my problem. I am not going to import 30 functions for use. That is crazy. I need to import a class, so I can call all of the functions without references. – Bit Fracture Feb 19 '13 at 04:41
  • Thus the wildcard: `import static TESTPACKAGE.TestClass.*;` ...it's right there in my answer. – Matt Ball Feb 19 '13 at 04:44
  • I know you guys probably think I am stupid, but I am still getting errors. Cannot find symbol: Class: TestClass. Your code didn't work. And I know, it just should work. – Bit Fracture Feb 19 '13 at 04:45
  • Solved. Using the **refracting** option, suddenly everything started working. Also changing a few things to public after that removed all remaining errors. Thank you all for helping! – Bit Fracture Feb 19 '13 at 04:51
0

If you're using netbeans then you won't be finding any issue during the import of any java classes. And yeah that's true you can have only "pre-compiled classes" as import statement. If you are running on notepad then you need to compile your independent classes first and then your dependent classes. And if you use Netbeans or Eclipse or any other IDE then you do not have to worry they will manage by themselves, you just have to use proper package and class names
You can have two types of imports

import package1.Class1;
import static package1.Class2;

With the first one you can create object of Class1 (or any other class if present in Class1) and invoke the methods
With the second one you can directly call the static methods of Class2 without referencing it with classname

Updated
See tutorials on packages in JAVA

asifsid88
  • 4,631
  • 20
  • 30
  • How do I get the package name that my project is in? Because the package is the same for both the class I am importing and the class being imported to. – Bit Fracture Feb 19 '13 at 04:24
  • when you write your java code package name is written as the first statement.. See your java code where they reside `package p1;` Something of this sort will be present at the start of your java code. If this statement is missing then you have your classes in _default_ package. Don't do that (bad practice) – asifsid88 Feb 19 '13 at 04:26