aspose file tools*
The moose likes Beginning Java and the fly likes Static Imports doubt! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static Imports doubt!" Watch "Static Imports doubt!" New topic
Author

Static Imports doubt!

Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
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

Joined: Aug 16, 2005
Posts: 14272
    
  21

This looks a lot like your other question (about enums). Did you read the page behind the link I posted there about static import?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
@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

Joined: Oct 13, 2005
Posts: 39435
    
  28
It will be all in the Java™ Language Specification, here and here.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Jul 28, 2009
Posts: 45
Thanks guys.....finally got it!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
Instantiate a class so as to call a static method on it?
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
@ritchie

Bubsort2 ain't a static method.Look in the code from 1st post.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
No, it isn't. Sorry. My mistake.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

well no need for the static import also. YOu can use normal reference variable for static methods also.
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
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

Joined: Jul 28, 2009
Posts: 45
@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

Joined: Jul 15, 2009
Posts: 537

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

Joined: Oct 13, 2005
Posts: 39435
    
  28
I think it does make sense, but it is usually regarded as bad style.
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Is there a situation when would be forced to access static members using object references and not class name directly???

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39435
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Imports doubt!