Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (686)
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 2434 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: 165
Projects: 5
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:
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel

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

JGO Kernel

Medals: 409
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  
You cannot reply to this message, because it is very, very old.

Dwinin (63 views)
2015-11-07 13:29:08

Rems19 (77 views)
2015-10-31 01:36:56

Rems19 (69 views)
2015-10-31 01:32:37

williamwoles (104 views)
2015-10-23 10:42:59

williamwoles (91 views)
2015-10-23 10:42:45

Jervac_ (105 views)
2015-10-18 23:29:12

DarkCart (132 views)
2015-10-16 00:58:11

KaiHH (115 views)
2015-10-11 14:10:14

KaiHH (153 views)
2015-10-11 13:26:18

BurntPizza (167 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 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!