|
-
Dec 6th, 2004, 04:23 PM
#1
Thread Starter
Dazed Member
Can this code be right?
This is a code block that is posted under the docs for the java.util.Formatter class on sun's site. Ive never seen the static keyword used in an import statement and Calendar is a class, abstract, but still a class not a package so why the added .*?
Code:
// Format a string containing a date.
import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;
Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
// -> s == "Duke's Birthday: May 23, 1995"
-
Dec 6th, 2004, 05:15 PM
#2
Frenzied Member
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.
-
Dec 6th, 2004, 05:51 PM
#3
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 6th, 2004, 08:14 PM
#4
Thread Starter
Dazed Member
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);
}
}
}
-
Dec 6th, 2004, 09:12 PM
#5
Fanatic Member
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.
-
Dec 6th, 2004, 09:23 PM
#6
Thread Starter
Dazed Member
Re: Can this code be right?
-
Dec 7th, 2004, 04:23 AM
#7
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.
 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.*;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|