Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Static import conflict - toString method in Arrays and Object class.

 
Ranch Hand
Posts: 120
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to import the static members of Arrays class but it's conflicting with the some members in object class.

For example, I am trying to print the int array content using the toString static method in Arrays class, but the IntellijIDEA is telling me "Non-static member 'toString' can not be referenced from a static context."



If I follow the definition of toString method, IntelliIDEA takes me to toString method in Object class.

The code works if I use 'Arrays.toString' instead of 'toString'.

But how can I resolve the conflict without appending 'Arrays'?

Thank you!
 
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. You have to write Arrays.toString(...)
 
Quazi Irfan
Ranch Hand
Posts: 120
1
IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can't. You have to write Arrays.toString(...)



Thank you. Can you tell me why the Object.toString is being prioritized over Arrays.toString?
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Afraid not; I looked in the JLS (=Java® Language Specification) and the imports section didn't help. I think members are called in the order
Same class
Same package
Package java.lang
Packages imported
but I am not certain. Remember that toString() is the method in the same class/same object until proven otherwise. See if you can't find something about disambiguating names in the JLS. Because of the CapitalLettersConventions, the case of the identifier often resolves any possible conflict at compile time.
 
Sheriff
Posts: 14614
243
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looking in "Method Invocation Expressions" https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12

'the first bullet point under section 15.12.1 explains the "comb rule," which is what applies in this case.
 
Campbell Ritchie
Marshal
Posts: 66975
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the comb rule appears to apply here.
 
I am mighty! And this is a mighty small ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!