Snowman
Snowman

Reputation: 32071

Java using functions in a file

Ok really stupid question, but I have some methods defined in a file called x.java, which is in the default folder, and in the same folder, I have a file called z.java. I want to use the functions defined in x in z. When I try, it tells me the function is undefined. I tried to put import x.java; but it says x.java cannot be resolved. What am I missing here?

Upvotes: 0

Views: 114

Answers (3)

M.J.
M.J.

Reputation: 16666

I think, if the two classes are in same package then there is no need to import the class as that class can be used without any import and without any error.

Upvotes: 1

RonU
RonU

Reputation: 5795

Based on your description, I'd bet there's a good chance both of your source files defined classes in the default package (i.e., you don't explicitly define a package for them).

You can't import a class that's in the default package.

Recommend you put your class x in a named package (e.g., foo.bar.x), then you can import it:

import foo.bar.x;

Upvotes: 4

tim_wonil
tim_wonil

Reputation: 15806

if you have a file called x.java which compiles to x.class, you don't import by doing:

import x.java;

but you do

import x;

Upvotes: 1

Related Questions