Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm working on a class where it is supposed to access an Singleton's class methods using reflection. Are there any best practices in here or pitfalls? Thanks in advance
Well, there's the general "reflection is slow and should only be used as a last resort" best practice, but the guidelines that I follow, from simple to advanced:
Cache any Method or Field or Constructor instances you get from reflection lookups. Looking them up repeatedly is slow.
If you can, cache them globally in a WeahHashMap or similar that unloads them when the classes are unloaded so you don't leak class references from your cache
Even better, convert the Method objects to MethodHandles and then use LambdaMetaFactory to turn them into a Function<Object[],Object>, which will be almost as fast as a compiled method reference. Still cache the helper functions because creating them is expensive.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Some threading problems were solved without extending java.lang.Object.
The methods wait(), notify() and notifyAll() are implemented on Object, which seems to me not to be the best decision since it pollutes the class's interface. It affects all classes in Java, which I think should have been avoided.
Next, the synchronized block takes an instance of type Object, allowing us to accidently pass shared instances (e.g. String), which can cause problems. They could have crafted a class Mutex/Lock in order to avoid this.
I wonder whether this comes with any technical advantages - e.g. performance - or whether it is just bad design? Is there somewhere an official documentation, e.g. a JEP or something similar - on why the Java language designers decided to work directly with java.lang.Object?
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I have a class that has many fields made of objects of other classes. This class is used by multiple people who keep adding more fields to it according to their needs. I want to know if there's a drawback to this compared to having one collection field, say a Hashmap, in this class which can be used to contain other classes as and when necessary. This looks cleaner to me than declaring many fields which might end up not being used
A class with too many fields and methods is certainly harder to grasp and change later on - the shorter the class is, the easier it is to understand its uses.
On the other hand, keeping different class variables inside one hashmap in order to make the class shorter is not a good idea at all because you will lose type safety and will have to add many additional checks and castings later on.
In conclusion you should always keep the classes as simple and clean as possible without sacrificing best coding practices - perhaps instead of having so many different fields in one class you could have multiple smaller classes, each with their own responsibility, instead.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I was using this code to create some sort of a universal changer class:
//constructor method
public Change(Object affdObj, String affdField, float modifier) {
obj = affdObj;
//...
affectedField = affdObj.getClass().getField(affdField);
//...
affectedField.setFloat(obj, affectedField.getFloat(obj) + modifier);
}
But then I was advised to avoid reflection whenever possible since it's very slow. I was suggested to pay attention to interfaces. Unfortunately I can't see how to fit interfaces to my code.
Hence my question: if one needs to access a field which name he doesn't know in advance are there any options other than using reflection?
PS
Thank you for replies, guys.
And since my question is put on hold as primarily opinion-based, I consider this to be the answer to my question, i.e. there is no other way to achieve my goal which is better than mine in every aspect. In other words, I conclude that my approach is OK. Thank you.
First of all, reflection is not slow (anymore) and is widely used (Spring uses it, Hibernate uses it, etc.). So, you use it with confidence if your only concern is speed.
Regarding other ways to do what you want, since you provide the field name as as a string and identify it like that, you cannot do it with interfaces.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
What is the advantage of having static method and default method introduced in java 8 as i found it will add complexity and ambiguity in your code. Please bring some light on this.
The advantages are clear: static methods in the interface allow factories such as Stream.of to be placed right where they belong. Previously you would need a StreamUtil class or similar to hold them. Defender methods ("default") were an absolute must in order to introduce Stream-oriented goodness all around the Collections API, and are a very useful feature on their own, allowing free growth of API with convenience methods which only rely on other methods of the public API.
No complexity or ambiguity is added to your code, especially since the static methods are not inheritable.
The biggest adavantage of having default methods is that you can evolve an API for new features even it was already released without breaking the implementation of the users of the API.
Static methods make it possible to dispense with utility classes as the implementation can be written in the interface.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
We are learning about both of these things in Java class right now. I believe I understand the basic aspects of both, but not sure about how Casting ends up limiting Algorithm Reuse. Our teacher said we need to know this for the test next week. Can anyone explain this?
If you cast you are limiting your algorithm to only work with one Class (or it's children). If you were instead to use an Interface you would be able to accept a greater variety of Objects that themselves implement that Interface. Much more flexible.
Here is a related SO question: Explaining Interfaces to Students
When you use casting in your code, you must know the exact type that you cast to (during the code write phase). Hence your code can't be reused in the future with different types. Always remember to program to interface instead of to specific type.