Results 1 to 7 of 7

Thread: Can this code be right?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question 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"

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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); 
        }
       }
      }

  5. #5
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552

    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.

  6. #6

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.*;
    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
  •  



Click Here to Expand Forum to Full Width