File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Imports" Watch "Static Imports" New topic
Author

Static Imports

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
When


This means all the static members of Integer class will be imported.


What does this mean?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
That is wrong syntax, it means import Integer class statically that is not possible.


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


is this an incorrect import?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:

is this an incorrect import?


Yes, as outer class cannot be declared static ever.
Tell me your thinking over this, then I will be able to tell you more.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Then you can use this import statement in other classes to use InnerClass1.

import static OuterClass1.InnerClass1;

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Punit, in devaka Cooray's Simulator, I read you cannot import an entire class.
i.e import static java.io.*; is not correct
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Punit, in devaka Cooray's Simulator, I read you cannot import an entire class.
i.e import static java.io.*; is not correct


Yes I know this is not correct, I told you the reason behind that is static outer class is not possible.

Think this way, static keyword is there for just import static things, not non-static things.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
so,
import static java.io.BufferedReader.*; is correct?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:so,
import static java.io.BufferedReader.*; is correct?

Yes it is correct, now you tell me why and also what will be imported by this line?
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
io is the outer class, so it cannot be static.
It will import all static members of BufferedReader.
Right,Sir?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:io is the outer class, so it cannot be static.
It will import all static members of BufferedReader.
Right,Sir?


Wrong student, io is not a class. Now search and tell me what is io?
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
oops......I mean package.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:oops......I mean package.


So now tell me what is the meaning of
?
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Import all the static members(classes) of IO package.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Import all the static members(classes) of IO package.

That's why it is wrong.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Got it, Sir
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Got it, Sir

Great Grasshoper
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
1 more doubt, Punit

import java.io.BufferedReader.*;
Is this fine?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:1 more doubt, Punit

import java.io.BufferedReader.*;
Is this fine?


Ya, it is importing all non-static public methods, members, inner classes of the BufferedReader class.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit Singh wrote:Ya, it is importing all non-static public methods, members, inner classes of the BufferedReader class.

How? How can you use these methods, members, innerclasses of the BufferedReader class without an instance?
I thought import is only for classes, either a single class or all classes in a package.
I've never seen anything where import is class.*;
I see that it does not give any error. But what is the use?
Can someone explain with example?


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:
Punit Singh wrote:Ya, it is importing all non-static public methods, members, inner classes of the BufferedReader class.

How? How can you use these methods, members, innerclasses of the BufferedReader class without an instance?
I thought import is only for classes, either a single class or all classes in a package.
I've never seen anything where import is class.*;
I see that it does not give any error. But what is the use?
Can someone explain with example?


Yes, you are right Sachin, it is just a valid syntax, but of no use.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Punit Singh wrote:Yes, you are right Sachin, it is just a valid syntax, but of no use.


Nope! It has a use. It is used to import static and non-static inner classes. Look at this example





Now if you remove the import, then there will be error that cannot find symbol Inner and StaticInner ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Good Ankit, It seems my battery is bit down today.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Nice explanation Ankit and a good example too.......
Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
Ankit Garg wrote:remove the import, then there will be error that cannot find symbol Inner and StaticInner ...



Actually if you use

import myApp.Try.*;


it is called bad programming (from what I have been taught). You should always specify each and every class/member you want to import so that it will be clear when what is the dependency (quite useful while maintaining the code).

You should say:

import myApp.Try.Inner;
import myApp.Try.StaticInner;


-Piyush Porwal (Junk box)
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ok Guruji.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Piyush Porwal wrote:Actually if you use

import myApp.Try.*;


it is called bad programming


Did I say that it is good programming?? I was just saying that it is legal and it has a use as opposed to what punit said. I know that importing using * like import java.io.* etc is bad programming practice
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Piyush Porwal wrote: it is called bad programming (from what I have been taught).

Hi Piyush, thanks for your inputs. You maybe right, but the point here was about the use (ie. how to use the imports) and not about good programming.
Anyways, thanks for your reply too, even that helped(especially knowing about good programming).



Piyush Porwal
Ranch Hand

Joined: Apr 09, 2008
Posts: 30
No offense to your effort Ankit, I wanted to add more colors to what you mentioned.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Piyush Porwal wrote:No offense to your effort Ankit, I wanted to add more colors to what you mentioned.


Hey don't worry. I also didn't meant anything bad. But after writing what I wrote, I thought that it looks harsh so I added an emoticon in the end to show what was my expression when I was writing the response
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Imports
 
Similar Threads
static import
Doubt in static import
static imports
imports
static import rules??