File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes import static package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "import static package" Watch "import static package" New topic
Author

import static package

Cao Bin
Greenhorn

Joined: Apr 23, 2007
Posts: 14
import java.util.*;
import static java.util.Collections.sort;//#1

public class Sortee {
public static void main(String[] args){
List x = new ArrayList();
x.add(5);
x.add(new Integer(42));
x.add(new Integer(17));
x.add(Integer.parseInt("812"));
sort(x);
System.out.println(x);
}
}

In this java source.#1 use the static import to invoke sort() method.
what's the reason.
It's the first time saw static import.How to use static to modify package.
If we change the #1 to:
import java.util.Collections;
What else should we do to make this java source run well?
Thanks!


i love you not because who you are but because who i am when i'm with you
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40


In this java source.#1 use the static import to invoke sort() method.
what's the reason.
It's the first time saw static import.How to use static to modify package.


Basically, "static import" was a feature that was added with Java 5. With static import, you can import a static variable or static method of another class into your class. This way, you can use the static variable or static method, as if it was declared in your class.

IMHO, I think this confuses more than it helps... but I don't get a vote.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Cao Bin
Greenhorn

Joined: Apr 23, 2007
Posts: 14
I'm sorry to give so much trouble.
I hope this change will confirm with regulation.
It's my real name.
Anthony O'Dea
Greenhorn

Joined: Jan 18, 2007
Posts: 8
If we change the #1 to:
import java.util.Collections;
What else should we do to make this java source run well?


How do we call any static method?


Anthony O'Dea<br />BSc, PhD, DipIT, pre-SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import static package