aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static imports Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static imports" Watch "static imports" New topic
Author

static imports

Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Source: Whizlabs, but a bit modified for study

I have two files: test.java in package InnerClass and another file called test1.java.
Code for both is this:





If Line 2 in test1.java is commented, I get the error "cannot find symbol x".

I understand that we have to do a static import of x because x is declared static in test.java.

But my doubt is: Even the class Utilities is static in test.java. How come I am able to compile test1.java only with import InnerClass.test.*; ?

I mean, without doing a STATIC import of the static CLASS, I am able to use it. But, the same does not apply to the static VARIABLE x...Any differences?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

static import is needed to import only static methods and fields. import package.class.* is enough to import inner classes...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
import package.class.* is enough to import inner classes...


So, even for static inner classes, just a normal import package.class.* is enough is it?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

actually if you know there are no inner classes in java. So static inner class and non-static inner class are basically the same (they have some differences but it is not relevant here). So import package.class.* syntax is enough to import all the inner classes i.e. static and non-static...
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
ok Ankit..
But your quote " .........there are no inner classes in java. " what does it mean?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

well if you are really interested in knowing that, you can read my blog. the link is in my signature...
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Ok fine .. Thanks for the replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static imports