Static import - Netbeans error - java

The error, Netbeans gives me, is:
static import only from classes and interfaces
which is somehow strange for me, as this:
import org.lwjgl.opengl.GL11;
works fine while this:
import static org.lwjgl.opengl.GL11;
doesn't. Why is it not working for me?
BTW, GL11 is a class and I don't know why but Netbeans, when importing statically, thinks opengl is the class I want to import.

You wanna write:
import static org.lwjgl.opengl.GL11.*;
You are importing the members of the class, thus the * at the end.

Static import allows you to import static fields of other class. For example you can say
import static java.awt.Color.RED;
And then use RED in your class without mentioning class where it is defined.

Related

How to use static import on classes of same package in Java

Within my project package "pypapo.alphabet" I would like to have one class "alphabetStatic" that contains all frequently used variables (paths, directories, files, constants, etc...) as static final fields throughout the project.
In order to not fill up the code of the other classes with the "alphabetStatic"-prefix every time I access one of those static final fields I would like to perform some kind of "import static alphabetStatic".
I know that the import static statement refers to the classes of a package. However is it possible to import the fields of a class in such manner?
Nothing prevents you from importing package X from inside package X.
So
import static status pypapo.alphabet.alphabetStatic.*;
should definitely work for you.
I know that the import static statement refers to the classes of a
package.
Not really. It refers to static members of a class.
You can use import static with a fullquafiliedclassname.* (to mean any static member of the class) or with specific static field or method of a class.
For example to make an import static of a specific static field or method of a class, this is the syntax :
import static packages.Clazz.fieldOrMethod;
1) static field example
So you could do it to import the static out field form System :
import static java.lang.System.out;
and use it :
out("...");
1) static method example : same syntax.
import static org.junit.jupiter.api.Assertions.assertEquals*;
And use it :
assertEquals(expected, actual);
3) All static members of a class
Just suffix it with a wildcard :
import static org.junit.jupiter.api.Assertions.*;
Try this:
import static pypapo.alphabet.AlphabetStatic.*;
Note that classe names in Java must start with an uppercase letter.

Java packaging and import - where is my error?

Trying to learn Java from Bruce Eckel's book, I don't understand why the compiler doesn't find the library I want to import. I have first done this on Windows/Cygwin and now on Centos 7, using OpenJDK 1.8.0. Same result on both platforms.
This line:
import static t.b.u.*;
causes compiler error
$ javac TestPrint.java
TestPrint.java:2: error: package t.b does not exist
import static t.b.u.*;
^
I agree that package t.b doesn't exist, but I actually wanted to import package t.b.u. Why does the compiler ignore the u?
CLASSPATH is set as follows:
$ export|grep CLASS
declare -x CLASSPATH="/home/bbausch/thinking-in-java"
The package is a single file:
$ cat /home/bbausch/thinking-in-java/t/b/u/Print.java
package t.b.u;
import java.io.*;
public class Print {
... etc ...
The error is probably so obvious that I don't see it. Can somebody help?
This is specifically related to the Java Language Specification: https://docs.oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5.3
These two lines inherently ask for different things:
import static t.b.u.*;
This statement asks to import all static methods from a class named u from a package t.b.
import t.b.u.*;
This statement asks to import all classes underneath t.b.u.
Static imports target a TypeName only. Normal imports target a package, or a specific class.
The roughly equivalent general import to the static import would be this:
import t.b.u;
This asks to import just the class u from a package t.b.
In your specific example, you'd probably want this statement to import all static methods of the Print class.
import static t.b.u.Print.*;

glEnable(GL_TEXTURE_2D); Does not feel like working.... :(

So, I'm trying to apply a texture to my quad.
So i wrote this line of code : (line 35 in Artist.java)
glEnable(GL_TEXTURE_2D);
Which gave me this error :
GL_TEXTURE_2D cannot be resolved to a variable
I have these imports : (Hitting CTRL+SHIFT+O did not do anything)
import static org.lwjgl.opengl.GL11.GL_MODELVIEW;
import static org.lwjgl.opengl.GL11.GL_PROJECTION;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glMatrixMode;
import static org.lwjgl.opengl.GL11.glOrtho;
import static org.lwjgl.opengl.GL11.glVertex2f;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
I'm using Java, Eclipse, Mars. Anyone know why it won't work? :)
glEnable( GL_TEXTURE_2D) is a directive to the fixed-function
pipeline's shader generator that you want to include code to support
that texture unit
So if you use a modern opengl which has custom shaders, its option won't work ,and you won't need it.
For it to not error you need to import both GL_TEXTURE_2D and glEnable().
Like Sung Woo mentioned however, this function is deprecated and only needs to be used in the old OpenGL.

How do I import packages in Java?

I have a "Sprites" folder with some class files and a "Launcher" folder with some class files. I tried the following code for import:
package Sprites;
and it lead to the following
hw9\Launcher>javac *.java
TowerDefense.java:2: error: class, interface, or enum expected
package Sprites;
^
1 error
Am I doing this incorrectly? My Sprites and Launcher are in the hw9 directory, so I assumed it would work. A picture for clarification:
You can use a wildcard import to import all classes within the immediate directory:
import Sprites.*;
This opposed to something like:
import Sprites.Class1;
import Sprites.Class2;
import Sprites.Class3;
...
Generally, wildcard imports can produce conflicts and errors (for example java.awt.List and java.util.List), so usually better to avoid them.
Packages should also be lower-cased.
The error is due to syntax, usually when you see something like ...expected that is syntax error indicator.
In the class in your launcher package, include the import statements for the classes which are being referred to.
It should look something like the following:
package the.name.of.your.package;
import Spirites.NameOfclass; //quialify the import parth as is
class YourLauncherClass{
//class definition
}
Also make sure that semicolons aren't missing at the end of import and package.
Hope that helps.
Best practice is to import the specific class you require rather than importing the complete package.
import Spirites.NameOfclassRequired;
class YourClass{
//class definition
}
If you are using eclipse you can do that using CTRL+SHIFT+O When you do that eclipse imports the specific class you require. For an instance if you using an ArrayList rather than importing java.util.*; it will import java.util.ArrayList;
If you need multiple classes from a package then for sure you can import the entire package

Finding import static statements for Mockito constructs

I'm trying to crash through the brick wall between me and Mockito. I've torn my hair out over trying to get correct import static statements for Mockito stuff. You'd think someone would just throw up a table saying that anyInt() comes from org.mockito.Matchers and when() comes from org.mockito.Mockito, etc., but that would be too helpful to newcomers, no?
This sort of thing, especially when mixed in with myriad more import statements ending in asterisks, isn't always very helpful:
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
Yes, I know about and have been trying to use the Eclipse Window -> Preferences-> Java -> Editor-> Content Assist -> Favorites mechanism. It helps, but it doesn't hit the nail on the head.
Any answers to this question would be appreciated.
Many thanks,
Russ
Here's what I've been doing to cope with the situation.
I use global imports on a new test class.
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*;
When you are finished writing your test and need to commit, you just CTRL+SHIFT+O to organize the packages. For example, you may just be left with:
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyString;
This allows you to code away without getting 'stuck' trying to find the correct package to import.
The problem is that static imports from Hamcrest and Mockito have similar names, but return Matchers and real values, respectively.
One work-around is to simply copy the Hamcrest and/or Mockito classes and delete/rename the static functions so they are easier to remember and less show up in the auto complete. That's what I did.
Also, when using mocks, I try to avoid assertThat in favor other other assertions and verify, e.g.
assertEquals(1, 1);
verify(someMock).someMethod(eq(1));
instead of
assertThat(1, equalTo(1));
verify(someMock).someMethod(eq(1));
If you remove the classes from your Favorites in Eclipse, and type out the long name e.g. org.hamcrest.Matchers.equalTo and do CTRL+SHIFT+M to 'Add Import' then autocomplete will only show you Hamcrest matchers, not any Mockito matchers. And you can do this the other way so long as you don't mix matchers.
For is()
import static org.hamcrest.CoreMatchers.*;
For assertThat()
import static org.junit.Assert.*;
For when() and verify()
import static org.mockito.Mockito.*;
My imports
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
And it works
import static org.mockito.Matchers.anyInt;
e.g.
when(listMock.get(anyInt())).thenReturn("stackoverflow");

Categories

Resources