• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

imports

 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its working dear

regards,
Sharan
 
Vishal K Patel
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes....its working fine...!!!
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lovleen Gupta
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All rightie..
Thanks Souren.
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is working, DEAR
 
Thillakan Saba
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ANS :
2147483647
2a
 
Mohammed Niaz M.
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import static java.lang.System.out;
------

Im getting syntax error static 'Identifier ' expected.
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mohammed Niaz

Only both of us getting this error!!!
anybody else..???
Please help us..
Is it version problem???
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thiagarajan

I am using jdk1.4
 
Henry Wong
author
Marshal
Pie
Posts: 21420
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankith suresh:
Hi thiagarajan

I am using jdk1.4


Static imports were added in Java 5.0.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic