• 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

How to import fields or types with the same name using one Single-Static-Import Declarations?

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

JLS 8 7.5.3. wrote:It is permissible for one single-static-import declaration to import several fields or types with the same name, or several methods with the same name and signature.


I wrote a program where I used single static import and static import on demand declarations. I got about
  • static import of static variable and method.
  • static import of static variable and static method of a class having same name.
  • Explicitly importing always takes precedence over wildcard.
  • but I can't get this

    one single-static-import declaration to import

    does that mean we can import several fields, methods and class by just using Only one single static import declaration? If yes can anyone tell me how to declare it considering same below example please.
    c:\mypackage\MyRootClass.java

    c:\mypackage\mychildpackage\MyChildOneClass.java

    c:\mypackage\mychildpackage\MyChildTwoClass.java
    Output:
    childTwoStaticVarAge: 11
    Static method of MyChildTwoClass called
    commonStaticVarAge: 6
    Static method of MyChildOneClass called
     
    Marshal
    Posts: 74384
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do you mean importing overloaded static methods? You cannot have two fields with the same name? What about Arrays#sort? That is overloaded.Unfortunately it doesn't quite work like that for Arrays#toString because the compiler confused it with Object#toString.
     
    Campbell Ritchie
    Marshal
    Posts: 74384
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You cannot statically import the same method from two different classes and expect the compiler to distinguish the two.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Do you mean importing overloaded static methods? You cannot have two fields with the same name?



    No, what I mean is in quoted JLS point, they said it is permitted for one single static declaration(Hope they mean only one time single static import declaration) to import several fileds or types with same name, or several methods with same name etc.
    What I want to know, the way we can declare int variables on a single line using one int keyword like
    rather than using three times int is there any way to declare one single static import  declaration which import fields or types using only one time single static import declaration? If not then what do that JLS statement mean exactly?
    Yes I read about that, explicitly importing of two static fields or methods of two different class having same name results error cause of ambiguous name. I read JLS 8 Ch 7 and Java Tutorial Lesson: Packages thorough along with some combination of programs  



     
    Campbell Ritchie
    Marshal
    Posts: 74384
    334
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ganish Patil wrote:. . .  one single static import  declaration which import fields or types using only one time single static import declaration?

    No, no, no, definitely not.

    Unless you try
    import static java.util.Arrays.*;

    If not then what do that JLS statement mean exactly? . . .

    Didn't I show that earlier with the overloading of Arrays#sort?

    Maybe they realised that int i, j, k; is poor style and didn't allow similar contractions for static imports, which are a newer (Java5) feature. Maybe it was too hard to implement. Maybe (more likely) nobody thought about it. Nobody knows. Except maybe the old development team.
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I was also a bit sure about I misunderstood that statement but still tried every combination nothing worked except the original which they gave.

    Campbell Ritchie wrote:Unless you try import static java.util.Arrays.*;

    yes that's wildcard.

    Didn't I show that earlier with the overloading of Arrays#sort?

    Ohh you mean this? ya It was introduced by Tiger
     
    Ranch Hand
    Posts: 4716
    9
    Scala Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't know much about this but I have been reading about some languages that use commas a lot.
    Like x, y, z = 0
    I think it poor style also.
     
    Campbell Ritchie
    Marshal
    Posts: 74384
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Randall Twede wrote:. . .  x, y, z = 0 . ..

    You can even write that in Java®. I have a language where I can writeand you can writein Java®, too. I shall let you decide which is worse style
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    that's chain assignment in Java.
     
    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