Static imports == teh rox0rs

AFB

Lifer
Jan 10, 2004
10,718
3
0
Text

import static java.lang.System.out.*;


:cool:


println("That is all");

</post>

<revision id="1">

Of course using sparingly as it can make your source unreadable :)

</revision>
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
If I made up coding standards that other people had to follow static imports would be one of the first things I would ban. Talk about a readability hazard :confused:
 

ttown

Platinum Member
Oct 27, 2003
2,412
0
0
Originally posted by: kamper
If I made up coding standards that other people had to follow static imports would be one of the first things I would ban. Talk about a readability hazard :confused:
Totally agree.
Any java noob reading this needs to know that this is a bad idea 99.999999% of the time.
And if you think that maybe it's OK "in your case"... well it isn't, so don't even think about it.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
If I made up coding standards that other people had to follow static imports would be one of the first things I would ban. Talk about a readability hazard :confused:

Yeah, I agree. I'm the first to use the most verbose method possible (search for my thread on "this"). You never know though, it could be useful when you're just writing something quick and don't need to ever reuse it.
 

replicator

Senior member
Oct 7, 2003
431
0
0
I see this as creating more problems than those solved. Not a very useful feature IMHO.

C# has a much cooler feature, you can alias your imports.

using MyAlias = MyCompany.Proj.Nested;
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: replicator
I see this as creating more problems than those solved. Not a very useful feature IMHO.

C# has a much cooler feature, you can alias your imports.

using MyAlias = MyCompany.Proj.Nested;
I'm not quite sure exactly what your little demo implies (not a c++ progammer) but I think you're dealing with package names there? In java you don't have to use package names in your code after the import statements unless you have two classes with the same unqualified name. As for the problem demonstrated in the op, it could be shortened by saying:
PrintStream out = System.out;
...
out.println("hi there");
But that can be accomplished in any language.

What's really cool is Delphi's with blocks (not sure if it's inherited from pascal or not). You essentially create a new block, like an if, except that it "statically imports" an object(s) for the duration of the block so:

with (System.out) { //java syntax...
...println("hi"); // <-- resolves to System.out.println()
}

It's more useful because the import is limited to a readable size (not whole source file) and the syntactic savings are obvious if you have alot of work to do on a single object.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper

What's really cool is Delphi's with blocks (not sure if it's inherited from pascal or not). You essentially create a new block, like an if, except that it "statically imports" an object(s) for the duration of the block so:

with (System.out) { //java syntax...
...println("hi"); // <-- resolves to System.out.println()
}

It's more useful because the import is limited to a readable size (not whole source file) and the syntactic savings are obvious if you have alot of work to do on a single object.

Sweetness

Does it imply that everything is in the package or does it work like an import statement?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: kamper
What's really cool is Delphi's with blocks (not sure if it's inherited from pascal or not). You essentially create a new block, like an if, except that it "statically imports" an object(s) for the duration of the block so:

with (System.out) { //java syntax...
...println("hi"); // <-- resolves to System.out.println()
}

It's more useful because the import is limited to a readable size (not whole source file) and the syntactic savings are obvious if you have alot of work to do on a single object.
This is also one of those neat things you can do in VB.net (since it was found in the older VBs, MS brought it along for the ride) that you can't do in C#. I know that no one cares, but I thought I'd mention it anyway :)

 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: amdfanboy
Originally posted by: kamper

What's really cool is Delphi's with blocks (not sure if it's inherited from pascal or not). You essentially create a new block, like an if, except that it "statically imports" an object(s) for the duration of the block so:

with (System.out) { //java syntax...
...println("hi"); // <-- resolves to System.out.println()
}

It's more useful because the import is limited to a readable size (not whole source file) and the syntactic savings are obvious if you have alot of work to do on a single object.

Sweetness

Does it imply that everything is in the package or does it work like an import statement?
I won't pretend to know the exact Delphi implementation, but the concept is something like this.

Old way:
SomeItem.method1();
SomeItem.method6();
otherMethod(someItem.parameter57);

New way:
with (SomeItem) {
....method1();
....method6();
....otherMethod(parameter57);
}

Its not so much as importing a package as it is declaring to the compiler that if it runs into an unknown reference that it should check, in this case, SomeItem to see if it is there. If it is, use that one.
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Kilrsat
Originally posted by: amdfanboy
Originally posted by: kamper

What's really cool is Delphi's with blocks (not sure if it's inherited from pascal or not). You essentially create a new block, like an if, except that it "statically imports" an object(s) for the duration of the block so:

with (System.out) { //java syntax...
...println("hi"); // <-- resolves to System.out.println()
}

It's more useful because the import is limited to a readable size (not whole source file) and the syntactic savings are obvious if you have alot of work to do on a single object.

Sweetness

Does it imply that everything is in the package or does it work like an import statement?
I won't pretend to know the exact Delphi implementation, but the concept is something like this.

Old way:
SomeItem.method1();
SomeItem.method6();
otherMethod(someItem.parameter57);

New way:
with (SomeItem) {
....method1();
....method6();
....otherMethod(parameter57);
}

Its not so much as importing a package as it is declaring to the compiler that if it runs into an unknown reference that it should check, in this case, SomeItem to see if it is there. If it is, use that one.

That;'s what I thought, thanks Kilrsat :)
 

sandorski

No Lifer
Oct 10, 1999
70,886
6,421
126
SurfBot2000 extends sandorski;

WTF()
{
While (Confused>0) Do
{
NodAssuredly()
SlowlyMoveMousePointerToBackButton()
Click()
Confused := Confused-1
}
Else Post=('thoughts')
}

*****Syntax Error!*****

Dumping.....