• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Imports doubt!

 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt regarding staic imports.

Let's say we have a following class declaration :


To use these methods in another package, what import statements do I need provided that I need to use :
1. Bubsort1() alone
2. Bubsort2() alone
2. Both BubSort1() and BubSort2()


thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks a lot like your other question (about enums). Did you read the page behind the link I posted there about static import?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for static imports

syntax: import static <fully qualified package name>.<static member name>; only that static member or
import static <fully qualified package name>.*; all static members

1. <static member name> = BubSort1
2. import MyUtilities.Sort.BubSort2;
3. for both include both these.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@jesper: I know, but...

What I meant to ask was if I just included : then will I be able to access static members, or do I need to include static imports explicitly as well.

I would have tested it myself, but I won't have access to a compiler today, so....please help!

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will be all in the Java™ Language Specification, here and here.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes if you just include import MyUtilities.Sort.* or MyUtilities.Sort.Sorter then you can include the class Sorter and everything in it. But to access the static member use Sorter.BubSort1 and to access BubSort2 instantiate the class and then call it using the reference variable.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys.....finally got it!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instantiate a class so as to call a static method on it?
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ritchie

Bubsort2 ain't a static method.Look in the code from 1st post.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't. Sorry. My mistake.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well no need for the static import also. YOu can use normal reference variable for static methods also.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish Bangera wrote:well no need for the static import also. YOu can use normal reference variable for static methods also.


I don't quite understand what you meant by that.Can you please explain?

Thanks
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Nitish :
Did you meant to say that, we can use normal references to access static methods of the class. Like :

But that won't make much sense, will it?
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the thing about statics is that it can be accessed using the class name(no instance is required) and also if there is an instance of the class we can use it because all instances of that class will share the same copy of the static member. So all objects of the class will have the same static value...means the variable of the object can be accessed by the reference variable. Well it doesn't make sense but that is the other way you can use to access a static variable.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it does make sense, but it is usually regarded as bad style.
 
Deepak Borania
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a situation when would be forced to access static members using object references and not class name directly???

Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 50171
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean you would have an object and not know its declared type? Maybe somebody else can think of an instance when that would happen, but I can't.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic