Jump to content

Error when importing a java file in VS Code.

Gat Pelsinger

<Visual Studio Code> I have a main class in which I am trying to import a secondary class. Both the java files are in the same folder. If I just do "import <file_name.java>" then it doesn't recognize that file, and I have to create a package to be able to import, then in my main class if I do "package <name>" then VS Code gives me this error - The declared package "Package_name" does not match the expected package "".

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

Java imports by class, not by file. Furthermore, if both classes are in the same package, then you shouldn't need to import anything to have them work together.

 

Could you please post your code, so we can figure out what's going wrong?

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

I will explain my code structure. There is one folder in some directory, which only contains 2 java files and nothing else. The first java file is my Main java file which has the main method inside a class and the second java file has a function that I am writing inside a class. Till here, I have neither imported anything, nor defined anything as a package. Now, I have defined an array in my Main class, and I want to use that in the second class. Without any imports or packages stuff, the IDE (VS Code) doesn't recognize the reference to the array, and spits out an error saying to initialize it. Now tell me, how do I link these files to each other? Do I need to declare a package?

 

EDIT - oh wait, I am able to create an object of the class of the second java file, but I am not able to use the array in my Main java file from the second java file? I am doing something wrong and I think it's stupid.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

@dcgreen2k I guess I figured it out. I had to make an object and pass the array to the function of the second class. Stupid mistake.

Microsoft owns my soul.

 

Also, Dell is evil, but HP kinda nice.

Link to comment
Share on other sites

Link to post
Share on other sites

Good to hear you figured it out.

 

In general, to get data into an object from the outside, you'll need to pass the data as an argument. This can be done either in the constructor or in a method you call on that object.

 

To get data out of an object, you'll almost always use the return value of one of that object's methods.

 

To use methods inside a class, you will need to create an object unless that method is declared as being static.

Computer engineering grad student, cybersecurity researcher, and hobbyist embedded systems developer

 

Daily Driver:

CPU: Ryzen 7 4800H | GPU: RTX 2060 | RAM: 16GB DDR4 3200MHz C16

 

Gaming PC:

CPU: Ryzen 5 5600X | GPU: EVGA RTX 2080Ti | RAM: 32GB DDR4 3200MHz C16

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Hensen Juang said:

EDIT - oh wait, I am able to create an object of the class of the second java file, but I am not able to use the array in my Main java file from the second java file? I am doing something wrong and I think it's stupid.

Just post your code, please. Makes it much easier to understand what you're talking about.

 

To access the field of another class, you need an instance of that class. The field needs to be public (or package-private, if it's in the same package as your class). However, that is bad style. If a field should be accessible to others, you should expose it through a getter/setter, not directly.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×