Re: Can this code be right?
java 1.5 tiger added the static imports. Although I'm not sure why, unless it's just to shorten code in certain circumstances.
Re: Can this code be right?
That's exactly what it's for. It only imports static elements. Constants and functions, I think, or perhaps only constants.
Re: Can this code be right?
I don't think it's right.
Code:
import java.io.*;
import java.text.*;
import static java.util.Locale.*; // dosen't work
// import java.util.*; // works
// import java.util.Locale; // works
public class T{
public static void main(String[] args){
try{
BufferedReader buff = new BufferedReader(
new InputStreamReader(System.in));
String input = buff.readLine();
NumberFormat nf = NumberFormat.getInstance(Locale.CHINESE);
Number converted = nf.parse(input);
System.out.println(converted.shortValue());
}catch(IOException io){
System.err.println(io);
}catch(Exception e){
System.err.println(e);
}
}
}
Re: Can this code be right?
I'm thinking your editor doesn't support the syntax. I'm trying this with Eclipse 3.0 and yet it produces "Syntax error on token "stack"..." but when I do it with Textpad and compile with javac, it's ok.
Re: Can this code be right?
Re: Can this code be right?
And you're sure you're compiling as 1.5?
javac -source 1.5 T.java
to be sure.
Quote:
Originally Posted by JDK 1.5 docs
The static import construct allows unqualified access to static members without inheriting from the type containing the static members. Instead, the program imports the members, either individually:
import static java.lang.Math.PI;
or en masse:
import static java.lang.Math.*;