Alternatives to reflection when accessing an arbitrary field [closed] - java

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.

Related

Java method call format [closed]

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 5 years ago.
Improve this question
I'm just learning java, I found calling methods two ways the both work, but I'm not sure if there is a preferred way.
/**
* This method is called when the order button is clicked.
*/
public void submitOrder(View view) {
int price = calculatePrice();
String orderSummary = createOrderSummary(price);
displayMessage(orderSummary);
}
vs
public void submitOrder(View view) {
displayMessage(createOrderSummary(calculatePrice()));
}
would this be considered poor code? or wrong in anyway?
There is no standardized approach of invoking methods in a certain fashion.
It all depends on the developer writing the code or the team in your work place in general.
One of the most important factors to consider would be how readable your code is. If one approach is more readable than the other, then you could go with that.
Remember that when you develop code, especially at work, there would be several other developers who would maintain code in the future. So, readability and clarity and maintainability of code becomes important.
Hope this helps!
It's not wrong, but the second way is definitely harder to read and understand.
The first method is preferable as it will make your code much easier to understand and debug by you and others

Java: Is creating a "System" class a bad thing? [closed]

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
I recently started a project in Java, that contains a class called System. This class (Luckily) contains methods for output management, so in the rare cases where I need to use the System. methods (Or the System object in general) I just reference it as java.lang.System.. I believe that this could be looked down upon, as System could be looked at as a reserved name. I currently am in the beginning stages of this program, and could change it accordingly quickly, as there are little calls to the class itself.
While it's not illegal, you don't want to do this. If I were the next person working on your code, the first thing I would do is try to remove "java.lang" from "java.lang.System" and then get miffed when it wouldn't compile.
The idea is to go toward brevity and only write what you need to write, while making sense of it all for the next person. It's more an art than a science.
You could always name it something like ProjectnamehereSystem or OutputManager or something to that effect.
I would not create something so similarly named as an important class. While everything is easy to edit, you may be able to keep up with all the changes you are making.
But when the project evolves things will get messy and complex. I would suggest naming it something else that can be easily distinguished.

Declaring static method is allowed in java what are the advantage of doing so [closed]

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.

Modern OO practices in Java [closed]

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 8 years ago.
Improve this question
This is more of a conceptual question than technical pertaining to Java. I've noticed lately programmers tend to avoid creating classes to pass data around and simply move it using JSONObject if available or a basic HashMap. A lot of new interviewees choose to use what I call a JavaScript method of coding. So instead of creating class called Address, they would store it all in JSONObject/HashMap and pass it around as Object later casting to appropriate type. Also I notice a lot of
try { ... }
finally { return str; }
structured methods.
Is this something normal and accepted these days? To me, it just goes against everything I was thought in school. I mean, no generalization, no encapsulation, etc... And when asked, they claim JSONObject is encapsulation. Well, not disagreeing, but just not something expected.
The task is always being done in the end, but the way its done is disturbing to me. This kind of practice produces a lot of loose structures with a lot of potential errors if programmers are not careful. Wasn't java generics created to avoid exactly that?
Feel free to move this question to other exchange, but I'd really like to hear some opinions from seasoned folks.

How does Casting limit Algorithm Reuse in java? [closed]

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.

Categories

Resources