Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import static package

 
Cao Bin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Henry Wong
author
Marshal
Pie
Posts: 21399
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Cao Bin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic