java static import compilation error - java

Using java version 1.7.0_05
When i compile the below code it's give me testpackage could not be found error.
But if i remove the static keyword from "import static testpackage.TestStatic;" it's compiling successfully.
Test:
import java.io.*;
import java.util.*;
import static testpackage.TestStatic;
import static java.lang.Integer.MAX_VALUE;
public class Test {
public static void main(String args[]) {
System.out.println("hello world");
System.out.println("Maximum value of int variable using " +
"static import : "
+ MAX_VALUE);
}
}
TestStatic:
package testpackage;
import java.io.*;
import java.util.*;
public class TestStatic {
public static void testStatic() {
System.out.println("Inside Test Static");
}
public void testNormal(){
System.out.println("test normal");
}
public static void main(String args[]) {
System.out.println("hello world");
}
}

import static is for importing static members of classes, not whole classes. You could say "import static testpackage.TestStatic.testStatic;".
EDIT: fixed syntax

When you say import static testpackage.TestStatic; the compiler does not know what you want to import, you could mean import a static variable TestStatic in a class testpackage. In fact, I think you wanted to import testStatic() from the testpackage.TestStatic class,
For a method or field by name
import static testpackage.TestStatic.testStatic;
For all static methods and fields
import static testpackage.TestStatic.*;

Related

Can't import functions in java

I'm having a problem importing from a java class.
Here is my program:
import MyStuff.*;
public class Monday2
{
public static void main(String[] args)
{
p("\n\n\n\t\tGood Morning from the Morning2 Class.\n\n\n");
}
}
Here is the MyStuff class:
public class MyStuff
{
public static final void p(String inputString)
{
System.out.println(inputString);
}
}
Please tell me what I am doing wrong
You forgot magic word - static, after your import like this
import static MyStuff.*;
You can read more in appropriate topic.
Hope it helps!
Instead of import MyStuff.*; just use import static MyStuff.p; For more details please also see What does the "static" modifier after "import" mean?

How to import package in eclipse (Import package can not be resolved)

Package 1:
package Mypackage;
public class MyFirst {
int a;
MyFirst()
{
a=10;
}
}
Package 2:
package mySecondPackage;
import Mypackage.MyFirst;
public class MySecond extends MyFirst{
public static void main(String[] args) {
}
}
I got one error that is The import Mypackage cannot be resolved.
Then in the classpath of second project, add first project..
It must resolve your problem :)
Steps to fix it,
Open project properties(ALT+Enter) -> left side check for java build path --> Project tab right side and then add first project..
your superclass constructor is not visible that's why it is giving error..try this...
package pack1;
public class MyFirst {
int a;
public MyFirst()
{
a=10;
}
}
package pack2;
import pack1.MyFirst;
public class MySecond extends MyFirst{
public static void main(String[] args) {
MySecond s= new MySecond();
}
}

Access Class methods without prefixing with class

In a program like:
package testing;
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mouse.pressMouse(1);
}
}
Is there a way to call Mouse.pressMouse() without prefixing using Mouse?
You can import static methods:
import static com.company.Mouse.pressMouse;
public static void main(String[] args) {
pressMouse(1); // No need to prefix with "Mouse."
}
If the method pressMouse is static, then you could Static Import your method(s),
import MarcoLib.Mouse;
import MarcoLib.Timings;
import static MarcoLib.Mouse.pressMouse;
public class Testing {
public static void main(String[] args) {
pressMouse(1);
}
}
and per the link,
So when should you use static import? Very sparingly!
If the method pressMouse isn't static, then you could extend Mouse,
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing extends Mouse {
public static void main(String[] args) {
new Testing().pressMouse(1);
}
}

Java import class System

I have a question on class imports, It seems you can call a method with a reduced line if you have imported the class. I don't understand what is the name of this operation, and how is it possible...
For instance :
Why this code
public class test
{
public static void main (String args[])
{
System.out.print("Test");
}
}
Can be replaced by
import static java.lang.System.out;
public class test
{
public static void main (String args[])
{
out.print("Test");
}
}
What happens if you have also an object named "out" ?
Thanks in advance
What happens is that out from external class must be referenced by full name:
String out = "Hello World";
java.lang.System.out.println(out);
The variable out will shadow the static import and you will have to use the full name in order to use the function print.
import static java.lang.System.out;
public class Tester5 {
public static void main (String args[]) {
int out=0;
out.print("Test");
}
}
yields "cannot invoked print(String) on primitive type int. The same error is shown if out is an object.

Why am I getting an empty array of annotations here

According to the doc and to this answer I should be having "Override" (or something similar) in the following code:
import java.lang.reflect.*;
import java.util.*;
import static java.lang.System.out;
class Test {
#Override
public String toString() {
return "";
}
public static void main( String ... args ) {
for( Method m : Test.class.getDeclaredMethods() ) {
out.println( m.getName() + " " + Arrays.toString( m.getDeclaredAnnotations()));
}
}
}
But, I'm getting an empty array.
$ java Test
main []
toString []
What am I missing?
Because the #Override annotation has Retention=SOURCE, i.e. it is not compiled into the class files, and is therefore not available at runtime via reflection. It's useful only during compilation.
I wrote this example to help me understand skaffman's answer.
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.Arrays;
class Test {
#Retention(RetentionPolicy.RUNTIME)
public #interface Foo {
}
#Foo
public static void main(String... args) throws SecurityException, NoSuchMethodException {
final Method mainMethod = Test.class.getDeclaredMethod("main", String[].class);
// Prints [#Test.Foo()]
System.out.println(Arrays.toString(mainMethod.getAnnotations()));
}
}

Categories