Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Imports

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When


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


What does this mean?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is wrong syntax, it means import Integer class statically that is not possible.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is this an incorrect import?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

import static OuterClass1.InnerClass1;

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so,
import static java.io.BufferedReader.*; is correct?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
io is the outer class, so it cannot be static.
It will import all static members of BufferedReader.
Right,Sir?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops......I mean package.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi vijay wrote:oops......I mean package.


So now tell me what is the meaning of
?
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Import all the static members(classes) of IO package.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi vijay wrote:Import all the static members(classes) of IO package.

That's why it is wrong.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, Sir
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi vijay wrote:Got it, Sir

Great Grasshoper
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 more doubt, Punit

import java.io.BufferedReader.*;
Is this fine?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Ankit, It seems my battery is bit down today.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice explanation Ankit and a good example too.......
 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Guruji.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offense to your effort Ankit, I wanted to add more colors to what you mentioned.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic