Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Quick Question about Imports...  (Read 1399 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-02-23 02:51:14 »

Hey, I wasn't sure what to look up to find an answer for this so here goes. Why would you write something such as:
1  
2  
3  
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;


When you could just write:
1  
import java.io.*;


I see a lot of people importing specific classes instead of just using the general import statement, why do they do this and what, if any, is the benefit of doing so?
Offline Agro
« Reply #1 - Posted 2013-02-23 03:30:37 »

I think because everything else is included in that file too. Also because we don't generally type imports, Eclipse does it for us. Smiley

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-02-23 04:11:07 »

You'll end up importing too many classes, like Agro said, if you only use JFrame but do import javax.swing.* you've imported unnecessary like JPanel and JButton, etc. Look here: http://stackoverflow.com/questions/979057/any-reason-to-clean-up-unused-imports-in-java-other-than-reducing-clutter
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-02-23 05:24:09 »

If something that you think is better is not used by everyone else, the chances are there is something wrong with it.

Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #4 - Posted 2013-02-23 05:25:50 »

If something that you think is better is not used by everyone else, the chances are there is something wrong with it.
QTF
unless you're on the cutting edge of game development
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2013-02-23 06:03:06 »

There is no reason actually. Doing "import java.io.*" doesn't actually import all the classes at runtime. The JVM is too smart for that. It only imports referenced classes.

The only reason we "list" them out is Agro's response: Eclipse does it for us and it hides the import list Smiley

Offline philfrei
« Reply #6 - Posted 2013-02-23 07:02:30 »

I was told that listing all the imports individually slows down compilation or execution, not that this makes much sense. I'm not sure how reliable the source was, and never tested it, but I've taken to condensing the imports manually in my Eclipse environment. Am looking forward to the definitive answer, and am too busy/lazy to try and figure or test it out myself. Just tell me what to do. (I've got a lot else on my plate, thank you.)  Cranky

ra4king's statement about the compiler being smart enough not to load unneeded code has a ring of truth to it.  Smiley

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2013-02-23 07:03:16 »

Java's import doesn't do anything at all except shorten names.  Every class in your classpath is already available -- all import does is make it so you don't have to prefix it with the package.  It has absolutely zero overhead at any time.

Offline Danny02
« Reply #8 - Posted 2013-02-23 08:40:19 »

as sproingie said.

also, just let your IDE handle it for you. You can probably tell it also to use the star import if for example you use more then two classes from a package.

You only must use direct class imports if you want to use two packages which have some classes which are named the same.
Offline Damocles
« Reply #9 - Posted 2013-02-23 20:13:04 »

Its more a sign of clarity.

When you read a code from another programmer you can quickly see which specific classes are referenced,
and if anything might be missing, old version etc.
So when I look at source-code at my company, I can quickly see what classes and packages are important to that class.

You basically know what is important and what not to that class.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!