Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Naming Confliction

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import static java.lang.System.out; /* imports static 'out' variable */
import static java.lang.Math.*; /* imports all the static methods of 'Math' class */

public class ImportStatic
{
public static void main(String[] args)
{
out.println("sqrt "+sqrt(2.0));
out.println("tan "+tan(60));
}
}


while we import 'static' members, is there any possibility of naming confliction?
If there any,then how the compiler resolves it?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naming conflicts can happen with static imports. They occur in two basic scenarios:

1) When staticly importing 'all' statics from multiple classes

2) When staticly importing a method with the same name from multiple classes.


The compiler does not resolve the issue (which is why there is a conflict) so you have to. The solutions are as follows:
1) If the conflict came about because of using a wildcard to import static content from multiple classes, add a new static import line for the conflicted name you want to use.
Example:


2) Specify at the point of the call which implementation of the conflicted member you want to use (especially useful if you want to use one implementation in 1 line, and the other implementation in another line:


My feeling, though, is that if you have to worry about naming conflicts when using static imports you are probably over-using static imports. They are something I would suggest using sparingly, as they will probably cause headaches later on (now where did this value come from? Where is that method declared? etc...)
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic