1

I am familiar with Java but I am still trying to figure out some concepts.

Say I have FileA.java and FileB.java. Both files use the same import calls at the header of the file, for example:

FileA.java:

import java.util.Scanner;
import java.io.*;
{some code}

FileB.java

import java.util.Scanner;
import java.io.*;
{some code}

I have seen in other languages that when you have common libraries used by several source code files, then you abstract this to a shared file and then you can call this file in the source code files, thus helping reduce maintenance and have to retype them several times. So then for the example above we could do something like:

Common.lib:

import java.util.Scanner;
import java.io.*;

FileA.java:

include Common.lib;
{some code}


FileB.java:
include Common.lib;
{some code}

So I have the following questions and would appreciate if somebody could clarify since I'd admit I'm ignorant of the topic:

  1. Is this doable in Java?
  2. Is this a good idea/best practice? yes/no why?
  3. If it's not good practice then how do you deal having to type the same import headers for every single java file that use the same ones?
Seitaridis
  • 4,459
  • 9
  • 53
  • 85
wonitta
  • 93
  • 1
  • 7
  • possible duplicate of [Too many imports spamming my code](http://stackoverflow.com/questions/8485689/too-many-imports-spamming-my-code) – A.H. Jul 29 '12 at 14:50

2 Answers2

3

Is this doable in Java?

No.

Is this a good idea/best practice? yes/no why?

It is not doable without resorting to something like a preprocessor ... which makes this a non-Java solution, and definitely NOT "best practice".

If it's not good practice then how do you deal having to type the same import headers for every single java file that use the same ones?

I deal with it by using an IDE that allows me to hide the import statements when I don't want to look at them. Even in the old days when I wrote Java using a dumb(-ish) text editor, I never found this to be a significant issue. (But then again, after 30-something years of professional programming and 20-something different programming languages I've learnt to be tolerant of minor syntactic irritations.)


If you look at this early Java White Paper by James Gosling & Henry McGilton, you will see that one of the advantages of excluding #include (and typedef) is as follows:

"By removing all this baggage, Java becomes remarkably context-free. Programmers can read and understand code and, more importantly, modify and reuse code much faster and easier."

In other words, you don't have to navigate a maze of include files to figure out what a given identifier in the main source file refers to. That information is all in the class source file (assuming you don't use * imports ... which I don't).

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks the detailed clarifications/answers Stephen C. You totally understood my questions and concerns. – wonitta Jul 28 '12 at 18:11
0

Sadly, this is not doable in Java.

Antimony
  • 37,781
  • 10
  • 100
  • 107