Getting errors in import statement - java

Okay, first the project I am working on has two packages, first is murach.business which contains invoiceCalculations.java Second is murach.forms which contain InvoiceForm.java(JFrame) and SwingValidator.java
So, I am supposed to add Add import statements for the murach.business.InvoiceCalculations and java.text.NumberFormat classes in Invoiceformjava that i created from the scratch.
But, I am getting errors when i code that. netbeans says unused code for the below code:
have i written the code incorrectly? whats wrong? Please push in right direction.
import murach.business.InvoiceCalculations;
import java.text.NumberFormat;

"Unused code" doesn't mean that you have a syntax error. It just means that the import is not necessary, because the current code of the class doesn't use the imported class yet. If you add code inside the class that uses the imported class, the warning will disappear.
Note that, with current IDEs, you usually don't bother adding import statements manually. You use the class without importing it, and use a keyboard shortcut (or autocompletion) to add the necessary imports (and remove the unused ones) automatically. In Netbeans, the command is "Fix Imports (Ctrl-Shift-I)"

These are not errors but warnings that Netbeans gives you. The code should still compile fine

Related

Cant add import into Java class in IntelliJ

I have several simple classes in Java IntelliJ code, however soon as I finish adding any import statement that statement disappears. It let me type statement import java.util.* , but when I complete writing statement with ; in that moment whole statement disappears, with no error displayed.
This is probably because Settings -> Editor -> General -> Auto Import -> Optimize imports on the fly is enabled. IDEA finds an unnecessary import and removes it. So you should deselect it if you want to disable this behavior. However, I recommend not to organize imports manually. Just write your code and IDEA will import everything for you.
Adding Wild Card Imports import com.package.* is generally considered bad practice when writing Java as it clutters the local namespace that you're developing with. In this case,
Intellij recommends wildcard imports if you're using more than 5 classes from the same package in your current namespace, however that can be disabled too.
Source: here
All import statements were disappearing. In Setting>Editor>General>Auto Import I disabled Optimize imports on the fly option and import statements dont disappear anymore.
For the newer versions of Intellij, press Ctrl + Alt + Shift + s to open project settings, then choose Modules, under Dependencies tab choose the green + and add the folder in which your . class file is located. See if that helps

Why won't my IntelliJ import my local classes?

I've never had this problem before but check it out:
All of a sudden, IntelliJ tells me it wants to import these classes from org.springframemwork instead of my own.
Problem imports:
ViewNames, Mappings, AttributeNames
Example:
IntelliJ wants to import this
org.thymeleaf.engine.AttributeNames;
instead of my AttributeNames!
My solution:
File | "Invalidate Caches / Restart"
IntelliJ began getting an image of a non-existent directory structure and needs to be refreshed
Sometimes IntelliJ is simply "too quick" and adds an import for some obscure class. Happens to me from time to time, too.
The easiest solution: remove the wrong import, and in "organize imports" gets it wrong again, press option-enter. That suggests "import class", and typically gives you a list of choices to choose from, in case the class name is ambiguous.

Default imports from sun instead of java package in intelliJ

My current system(MacOS 10.10.5) configuration is :
IntelliJ IDEA 2017.2.2
#Built on 9August,2017
java version "9"
Java(TM) SE Runtime Environment (build 9+181)
Every time I start working on a project(currently 1.8.0_65) and write a class using
List<String> example = new ArrayList<>();
=> If I click on the auto suggested tip for List => Import class
The default import statement introduced is
import com.sun.tools.javac.util.List;
while I expect it to be :
import java.util.List;
Is there any configuration that I can redress or is this some bug in the combinations I am using? Would want to know what settings has given preference to sun package over the java package?
Note : Have gone through the answer of Any way (or shortcut) to auto import the classes in IntelliJ IDEA like in Eclipse? but the options that I get when I type List doesn't include the one from java.util in my case.
Edit : The import for java.util.List is not missing, I am able to import the same manually.
I had an issue like this with java.util.Arrays not showing up in completion lists. Turns out I had somehow added it to the import and completion exclusion list. My finger must have slipped in a quick action popup at some point.
Maybe the same thing happened to you. Open your Settings dialog and find your way here:
Then, look for this list of Excluded imports:
See if java.util.List shows up in that list, and if so, remove it.
With java.util.List excluded, com.sun.tools.javac.util.List might be the only other List type in your class path. If you have the "Add unambiguous imports on the fly" option enabled, IntelliJ would then import the sun class without even asking.
I don't think there is such option to prefer a package over another for importing classes.
Instead, you could exclude com.sun.tools.javac.util.List from auto import. To do so, in Class to Import window, click on the arrow on the right, then Exclude com.sun... from auto-import:
After that, java.util.List should be the first choice of import. If later you need to remove some excluded imports, you can go to Preferences › Editor › General › Auto Import to find them.

Java disutil CoordinateConversions import problems

I'm writing a Java class that will be used to send PDUs across a network- to do this, I am following the tutorial at: Tutorial
In the example, the line:
double lla[] = CoordinateConversions.xyzToLatLonDegrees(c);
appears towards the end of the class, and I see that CoordinateConversions has been imported with the line:
import edu.nps.moves.disutil.CoordinateConversions;
I have tried using the xyzToLatLonDegrees(); method in the class that I am writing- calling it in the same way as is done in the example. However, for some reason, I get a compile error that says:
CoordinateConversions cannot be resolved
on the line where I'm trying to use it, and
The import edu.nps.moves.disutil.CoordinateConversions cannot be resolved
on the line where I am importing it.
Does anyone know why this is, and how I can fix the import, so that I can use the xyzToLatLonDegrees() method?
You need to have the CoordinateConversions class on your classpath. Either by obtaining the source and dropping it into your project (possibly adjusting package names, and only if the license allows), or by finding a JAR containing that class and adding it to your build path in your IDE.
You probably need to download the Java files from here.

what is the need for importing libraries multiple times

in most code examples I see people doing this.
import javax.swing.*; // for the frame
import java.awt.*; // for the checkBox and the label
import java.awt.event.*; // for the checkBox listener
If I am correct when we say import java.awt.* it imports everything inside it, so there wont be a need to say import java.awt.event.*; or is there a speed improvement? can anyone also explain what importing a library does, is it importing a simple text class to be included in the source or telling jvm to include the byte code of whatever is imported? so importing in java does nothing but switch the namespace, so I dont have to type long class names?
Forget the term subpackage. Do it quick. It does not exist in java world.
java.awt is a package (namespace), java.awt.event is another one and they have nothing in common. Their names share some characters, but the packages are totally unrelated. The import statements imports a class or some classes from exactly one package (namespace). If you need classes from a different package (namespace), then you have to add another import statement.
BTW, in response to a comment to another answer: You do not have to use import statements. If you don't use them, you simply have to use the fully qualified classnames in your java source file (except: classes from java.lang and the current package are imported automatically). So import could be considered as a convenient way to keep the code readable.
Importing is not required in order to use a class in your source file.
The line...
import java.awt.*;
...doesn't mean that all subpackages will also be imported. You have to explicitly import every package. As an example, importing java.* doesn't give you the entire java library.
For what it's worth, I recommend importing the specific classes only unless you have good reason to use *.
Importing a package doesn't import its subpackages.
Importing is about switching the namespace, too.
If you only had import java.awt.* and you were to use the class java.awt.Outer.Inner, everywhere in your code you have to refer to it as Outer.Inner.
By contrast, when you say import java.awt.Outer.*, you can refer to the inner class as just Inner.
Importing is just a compile time feature, in bytecode you will find only direct references to specific classes, everytime its instance is used.
"import" constructs exist just to eliminate use of full class name everytime.

Categories

Resources