• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Import explained  RSS feed

 
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot quite understand the static import.
A normal import , imports the class withouth the static members
A static import , imports only the static members od the class?

So, if I write for example



This code prints swing swing 10 , but it confuses me. I cannot understand the imports and the logic behind them
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A regular import allows you to reference a class by it's name, without the whole path.  The static import allows you to reference the static members of the class and not have to prefix their names with the name of the class they belong to:

Without the import statement, you'd have to do this:

That can be quite annoying if you have many references to rope.Rope and it makes your code unnecessarily verbose.

If you don't have a static import, you'd also have to write this, even if you have a regular import:

Normally, that's fine because the "Rope." prefix gives the reader context and makes it clear where the LENGTH value and the swing() method are coming from. By using a static import, you can avoid having to put the "Rope." prefix and just write:

This may or may not be better, depending on whether the fact that LENGTH and swing() are provided by the rope.Rope class can be easily inferred from the rest of the code.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import statements are really about convenience and brevity.  Before static imports were introduced to the Java language, you had to explicitly prepend the class name to which static members belonged, so in your example, you would have always had to write Rope.LENGTH and Rope.swing().  As with regular imports, it's preferable to write explicit import statements rather than something like import static rope.Rope.*:

With explicit imports, it's easier to see where the imported names are coming from.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I still don't get the difference between the import and the static import.

For example I have a class named Rope in the package rope. It has a static variable and a static method and a non-static method




And then I have another class named Chimp in another package (package name : otherPackage)



Now if I write static import, it means ONLY the static members are imported, in this way the LENGTH and the swing method are imported.



In order to use the static members without using the fully qualified names, I make the static import, but I cannot make an instance of the class and I cannot use the instance variables of the class.
If I wanted in this case to make an instance of the class I should write:




 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ability to write code like what you did on lines 23 & 24 has less to do with either kind of import statement than it does with the fact that it's legal to access static members of a class via an instance reference. Doing so, however, is poor form and you should avoid the practice. If that's the only thing that's confusing you then it's because you're mistakenly making a connection where there is none.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Your ability to write code like what you did on lines 23 & 24 has less to do with either kind of import statement than it does with the fact that it's legal to access static members of a class via an instance reference. Doing so, however, is poor form and you should avoid the practice. If that's the only thing that's confusing you then it's because you're mistakenly making a connection where there is none.


Thanks Junilu, I am not intending to use this, I just want to know all the possibilities so I can have it cleared inside my head.
So the main difference between the two imports , as I have understood, is that when using static import I can call all the static variables and methods from the class directly, without using the fully qualified name, but I cannot make an instance of the class and I cannot use the instance variables-methods.
When I use normal import I can use the static variables and methods only by using the fully qualified name and I can create an instance of the class therefore use the instance variables and methods.
Is that correct??
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
When I use normal import I can use the static variables and methods only by using the fully qualified name and I can create an instance of the class therefore use the instance variables and methods.
Is that correct??


To be pedantic, a "fully qualified name" is everything that is needed to reach the variable/method. This includes the package and the name of the class.  With "import", you just need the name of the class, you do not need the name of the package -- as the compiler knows what it is from the import.

Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Ioanna Katsanou wrote:
When I use normal import I can use the static variables and methods only by using the fully qualified name and I can create an instance of the class therefore use the instance variables and methods.
Is that correct??


To be pedantic, a "fully qualified name" is everything that is needed to reach the variable/method. This includes the package and the name of the class.  With "import", you just need the name of the class, you do not need the name of the package -- as the compiler knows what it is from the import.

Henry


Ok thanks a lot !! that is what I meant
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:when using static import I can call all the static variables and methods from the class directly, without using the fully qualified name, but I cannot make an instance of the class and I cannot use the instance variables-methods.

It's best that you stop at "without using the fully qualified name" and not conflate the rest of that sentence as a direct consequence of using a static import. Again, don't try to make a direct connection where there really isn't one.

When I use normal import I can use the static variables and methods only by using the fully qualified name and I can create an instance of the class therefore use the instance variables and methods.

Again, you are trying to make a direct relationship where there is none. 

Your line of reasoning is like saying "If I can find the keys to my car, then I won't be hungry."  What's the connection between having keys to your car and not being hungry?  Well, you might reason that if you had the keys, you can start your car, then you can go to the store and buy some food, you can come back home, you can cook the food, then you can eat, and then you won't be hungry! There's really only roundabout way to find a connection between having keys and not being hungry, right? The only direct relationship in that line is "If you have the keys to your car, then you can start it." That's it. Full stop.

Likewise, the only direct consequence of a regular import statement is that you can reference a type (class/interface/enum) that exists in a different package without having to use the fully qualified name all the time. That's it, full stop. If you really want to go yak shaving, then you can continue and say that because you can reference the name of a type, you can declare a variable of that type, create instances of that type, access members of that type, and do whatever else you can do with a reference to that type.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!