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 imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "imports" Watch "imports" New topic
Author

imports

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63


In this program -- they are importing static System.out and using only out.println..
Also, they are importing static Integer.* and using MAX_VALUE..
However, if we import staic Integer and use Integer.MAX_VALUE..why does it not work??
Please explain.

Thanks,
Lovleen.
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
its working dear

regards,
Sharan
Vishal K Patel
Ranch Hand

Joined: Oct 20, 2006
Posts: 43
yes....its working fine...!!!
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
rigth you are..its working..
but my question is why will it not work if we do import static java.lang.Integer instead of java.lang.Integer.* ?

Thanks.
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
Just think about it. What's the purpose of a static import ? To import the static members of a particular class.

When you write you are telling the compiler that you want to import all the static members of Integer class so that you can call them directly without having to write "Integer." infront of them.

But when you try to write you are actually telling the compiler to import a static top level class & if you remember, top level classes can never be static which means that you cannot ever import a top level class which is static. But by writing the preceding you are trying to do just that & hence the compiler wont allow you to do so.

Regards,
Sourin.


Regards,<br />Sourin.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Originally posted by Sourin K. Sen:
Just think about it. What's the purpose of a static import ? To import the static members of a particular class.

When you write you are telling the compiler that you want to import all the static members of Integer class so that you can call them directly without having to write "Integer." infront of them.

But when you try to write you are actually telling the compiler to import a static top level class & if you remember, top level classes can never be static which means that you cannot ever import a top level class which is static. But by writing the preceding you are trying to do just that & hence the compiler wont allow you to do so.

Regards,
Sourin.


All right, Sourin..Got your point..
But why does it then work in case of import static System.out?
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
That's because out is a static field in System class. Again, the static import, imports only the static members in a class, be it a field, a method or a static inner class.

If you write it this way :
import static java.lang.System.out;

it means you are telling the compiler that you want to import only the static "out" field in the System class & you dont want to import any other static member of that class.

This is just a way of importing static members individually.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
All rightie..
Thanks Souren.
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
import static java.lang.System.out;
import static java.lang.Integer.*;

class staticimport11{
public static void main(String[] args){
out.println(MAX_VALUE);
out.println(toHexString(42));
}
}

For me the above code is not compiling...
I am getting the following error...help me

---------- compiler ----------
staticimport11.java:1: <identifier> expected
import static java.lang.System.out;
^
staticimport11.java:1: '.' expected
import static java.lang.System.out;
^
staticimport11.java:2: <identifier> expected
import static java.lang.Integer.*;
^
staticimport11.java:2: '.' expected
import static java.lang.Integer.*;
^
4 errors

Output completed (0 sec consumed) - Normal Termination
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
It is working, DEAR


SCBCD, SCJP & MCP
HowToAskQuestions
Thillakan Saba
Ranch Hand

Joined: May 15, 2007
Posts: 53
ANS :
2147483647
2a
Mohammed Niaz M.
Greenhorn

Joined: Apr 28, 2007
Posts: 27
import static java.lang.System.out;
------

Im getting syntax error static 'Identifier ' expected.
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Hi Mohammed Niaz

Only both of us getting this error!!!
anybody else..???
Please help us..
Is it version problem???
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

If its a version problem we will get the error like this

C:\practice\basics>javac -source 1.4 static1.java
static1.java:1: static import declarations are not supported in -source 1.4
(try -source 1.5 to enable static import declarations)
import static java.lang.System.out;
^
static1.java:4: variable-arity methods are not supported in -source 1.4
(try -source 1.5 to enable variable-arity methods)
public static void main(String... args)
^
2 errors


Thanks

Anil Kumar
thiagarajan saravanakumar
Greenhorn

Joined: May 31, 2007
Posts: 9
yes it is working fine and i got output as
2147483647
2a
it is better to check verison ,
have you installed jdk 1.5 or not??
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
Hi thiagarajan

I am using jdk1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

Originally posted by Ankith suresh:
Hi thiagarajan

I am using jdk1.4


Static imports were added in Java 5.0.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: imports