Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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 6414 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:

When you could just write:

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

Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 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:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel

Medals: 383
Projects: 11
Exp: 4 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.

Offline Jimmt
« League of Dukes »

JGO Kernel

Medals: 167
Projects: 5
Exp: 6 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.
unless you're on the cutting edge of game development
Offline ra4king

JGO Kernel

Medals: 508
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*" 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

music and music apps:
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  

Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4585 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4016 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 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‑
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!