This question already has answers here:
Why is using a wild card with a Java import statement bad?
(18 answers)
Closed 9 years ago.
I am a student, and a couple of the books I have been reading (Java for Dummies, for one) has said using the wildcard import statement is bad programming practice and encourage the reader to avoid using it. Whereas, in class, we are encouraged to use it. Can somebody please explain why it is poor programming practice?
If so, what adverse affects does it have on the program performance? For example, slow it down.
The more you insert, the higher the change that you will get a naming collision where two classes have the same class name:
http://en.wikipedia.org/wiki/Name_collision
The first example i can find within the java API are:
http://docs.oracle.com/javase/6/docs/api/javax/naming/Binding.html
http://docs.oracle.com/javase/6/docs/api/org/omg/CosNaming/Binding.html
Related
This question already has answers here:
Where can I find the Java JDK source code? [closed]
(11 answers)
Closed 6 years ago.
In java, is there any way to access the methods to classes that already you import? For example, is there a way to view the code for all the methods used for arrays? Such as the constructors, add(), remove(), size()? I have checked oracle, but there is no code, only method names and parameters. I understand how the methods work, but i'd like to see the actual code used.
Search for the JDK source code, depending on the version you want.
This question already has answers here:
What is the exact meaning of static fields in Java?
(4 answers)
Closed 7 years ago.
I am reading a code one of my friends wrote to store the account of users using arraylist. So he declared private static ArrayList = new ArrayList<>(); I understood this part, but I am not sure why he has declared it as static. That brought me up to this question : When is it a good idea to declare a data structure static?
That's a question that calls for a very delicate answer, but:
When is it a good idea to declare a data structure static?
NEVER
Static members are the root of many evils. They make the class less reusable. They make the class harder/impossible to test. They make the class non thread safe by default, which can be remedied but it's difficult to get right and can cause performance issues. They put the responsibility of managing the resource in the wrong place.
This is a very big OOD topic that's been discussed to great extents, so I'll just point out a commonly used acronym that is worth looking up: SOLID principles.
This question already has answers here:
What is a serialVersionUID and why should I use it?
(25 answers)
Closed 7 years ago.
I was following the article http://javapapers.com/core-java/serialversionuid-in-java-serialization/ to understand Seriliazation.
In this its said that Java serializationUID recommended to be unique. I dont understand the reason for that.
Can someone please explain?
In this its said that Java serializationUID recommended to be unique. I dont understand the reason ...
It's wrong. There is no reason not to use 1L for every class, except in the case where you are retrofitting to a class that was initally defined without one, in which case you have to use what the serialver tool tells you.
As a matter of fact you can't use a unique value for every class, because it's a hashcode.
Don't rely on arbitrary Internet junk like this. Use the Javadoc and the specifications.
This question already has answers here:
Why is using a wild card with a Java import statement bad?
(18 answers)
Closed 7 years ago.
In some of the resources I have seen online about Java they say that it is not good to use the * in an import statement in Java. For example import java.io.*
I was wondering why this is considered bad practice. Is is solely because it makes it hard for another programmer to know exactly what classes you are using under the java.io package or is there another reason for it?
Because Java.io.* imports the entire IO class. This means the program will be loading components that it won't need.
This question already has answers here:
Why is using a wild card with a Java import statement bad?
(18 answers)
Closed 8 years ago.
Is using
import java.util.*
not favorable compared to calling on the specific packages? I was wondering if its significantly inefficient to a program to call on all the packages, rather than listing them out specifically such as:
import java.util.Scanner;
import java.util.Math;
etc. I am preparing for interviews and want to make sure I have good coding practices.
The problem with importing * is that it increases the chance of naming conflicts.
Let's assume that in your program, you have a class called EventListener, since java.util also has a class called EventListener, right the way you have some conflicts to deal with, but you do not even care that java.util.EventListener in this context.
This really can be avoided, simply by not importing * and only importing specific classes that are truly needed.
#PeterPeiGuo is right, besides that, I want to say I don't use import xxx.xx.*, cause as a developer you need to know which class you should use and which class not. You need to understand everything for your code/application. So import xxx.xx.XXX is better from my view.