Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sybex OCA Study Guide Errata  RSS feed

 
João Victor Gomes
Ranch Hand
Posts: 83
7
Eclipse IDE Java Postgres Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, here I am again.

I believe there is an error in the explanation of static imports on page 188 of the Study Guide.

The authors say "The compiler will complain if you try to explicitly do a static import of two methods with the same name or two static variables with the same name."
And then, they show two static imports of two static elements with the same name, but in different classes, saying that it won't compile.
But it actually does compile, like this



If we import static elements with the same name, the code will compile if we don't try to use the imported static element, like in the code above.
If we try to use the static element, like in the following example, then the code won't compile, because it would be an ambiguous call.

Finally, if we change the code above, using the classname to specify the static element to be used, then the code will compile again, like this

Is that right?

Thanks again.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
João Victor Gomes wrote:Is that right?

Since a static import is nothing more than a shortcut (less typing) for a static method or variable. So if you don't use the shortcut, there is no reason why the compiler would complain.

Please note that the behavior is different for regular imports. If you try to import classes with the same name from two different packages, you'll get a compiler error (even if you don't use the simple name of these classes in your code)If you know why, you might earn some meat (an additional cow)?
 
João Victor Gomes
Ranch Hand
Posts: 83
7
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
João Victor Gomes wrote:Is that right?

Since a static import is nothing more than a shortcut (less typing) for a static method or variable. So if you don't use the shortcut, there is no reason why the compiler would complain.

Please note that the behavior is different for regular imports. If you try to import classes with the same name from two different packages, you'll get a compiler error (even if you don't use the simple name of these classes in your code)If you know why, you might earn some meat (an additional cow)?


I must confess, you got me this time.
Tried to figure out why it happens, I would say that Java always uses fully qualified name, and the import is a way of making things easier. But it would be the same as the "shortcut" that you mentioned for static import.
 
Roel De Nijs
Sheriff
Posts: 11077
173
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
João Victor Gomes wrote:Tried to figure out why it happens, I would say that Java always uses fully qualified name, and the import is a way of making things easier. But it would be the same as the "shortcut" that you mentioned for static import.

Here's a hint/tip based on your code examples: in class SomeClass you currently have a method doSomething. Now simply create in AnotherClass a static variable doSomething (and - of course - remove the static method with the same name) and try to use both of them through a static import and see what happens...
 
João Victor Gomes
Ranch Hand
Posts: 83
7
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
João Victor Gomes wrote:Tried to figure out why it happens, I would say that Java always uses fully qualified name, and the import is a way of making things easier. But it would be the same as the "shortcut" that you mentioned for static import.

Here's a hint/tip based on your code examples: in class SomeClass you currently have a method doSomething. Now simply create in AnotherClass a static variable doSomething (and - of course - remove the static method with the same name) and try to use both of them through a static import and see what happens...


Well, now it works when I use the static element directly. So, now the compiler knows that one element is a method and the other is a variable.



The output is:

The code compiles and runs perfectly.
Playing board games
Doing something...



Now, I'm scratching my head about the regular imports. Trying to find something in the Java Language Specification.
I'm afraid this is one of those things that is right in front of my face, and I'm trying to turn it into a complicated thing
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!