Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set the default package in java?

Tags:

java

We know java.lang is the default package in java, and we don't need to import anything from them.

  1. Is there any other package(s) like java.lang ?
  2. Can I add another default package with the java.lang ?
  3. Can I replace java.lang with any other package (I mean, If I re-write all the classes and interfaces from java.lang to java.lang1, then can I set java.lang1 as default) ?
  4. Can I change the base class (java.lang.Object) to some other class ?
  5. If above listed things are possible where should I write code for that ? (Is that will be a java code snippet or some kind jvm or compiler specific code ?)
like image 270
zumit Avatar asked Aug 12 '14 06:08

zumit


1 Answers

I'm sure enough that all of these are not possible to just post, "no, these are not possible." Note you can't do it with reflection since these are all compile-time questions.

(4), "can I change the default base class from Object," is not like the others, and is the most ludicrous. That would break many many programs at compile, because even doing something like System.out.println(user) relies on the fact that the object user has a toString method. In particular String is broken, because its toString method is inherited from Object.

Everything else you asked is compile-time only, so less ludicrous, but still not possible.

For completeness, it also would be strange and bad practice. I've heard of a C programmer who included #define retrun return in a headers file. So when any other developer maintaining his code saw retrun eta; they had to dig out the headers file to know what it meant. This is write-optimized code but software engineering is about read-optimizing code (yes I'm generalizing; trying to cover this in a paragraph). Cluttered imports do that - it makes it very obscure to tell where things come from, which is fine for String and Exception, but problematic even for things in java.util. Yes, this line was arbitrary at some point, but I feel it's intelligent and certainly should not include more things.

like image 120
djechlin Avatar answered Nov 05 '22 04:11

djechlin