• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • 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: 56518
    172
    • Mark post as helpful
    • send pies
    • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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: 56518
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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
     
    Randall Twede
    Ranch Hand
    Posts: 4696
    8
    Java Scala
    • Mark post as helpful
    • send pies
    • 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: 56518
    172
    • Mark post as helpful
    • send pies
    • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    that's chain assignment in Java.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!