Questions tagged [java]
Java is a high-level, platform-independent, object-oriented programming language originally developed by Sun Microsystems. Java is currently owned by Oracle, which purchased Sun in 2010.
4,985 questions
2
votes
0
answers
104
views
When Should We Separate DTOs from REST API Serialization Classes? [closed]
We know that combining a domain entity, a DTO, and a REST API serialization class into one won't pass code review:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@Entity
@Table(name = "...
3
votes
3
answers
967
views
Should business logic classes be POJO only?
I read about three-tier architecture and I have a question: I read that in business logic (that is, what is in logic tier) there should be POJO classes, but in most Spring manuals these classes are ...
0
votes
1
answer
73
views
Feasibility of using different java version for different project with different compatibility properties
I have multiple Flutter Android app projects that have different compatibilities of Java.
Now, what should I do if I am developing more than one project simultaneously?
If the first one uses Java 17, ...
3
votes
5
answers
3k
views
The best way to handle exceptions?
I have the following method, which needs to return a List, but exceptions might occur along the way, and I don't want to handle them in my application, mainly because I don't even know how to handle ...
0
votes
2
answers
321
views
How does the MVC pattern actually work?
I’m still a Computer Science student, and recently I’ve had to develop a project using the MVC pattern, but without having learned in depth about how it actually works. And it’s not the first time I’...
2
votes
1
answer
194
views
checkNotNull vs. JEP 358: Helpful NullPointerExceptions: Should we remove existing null checks?
With the introduction of JEP 358 in Java 14, which provides more informative NullPointerException (NPE) messages, is it advisable to remove existing explicit null checks in cases where the null-check ...
2
votes
3
answers
895
views
Is breaking encapsulation a necessary compromise for serialization?
I've been considering the way to solve this problem for a while, and I'm currently stuck between two options I both feel are suboptimal. Here's an abstract example of what I'm dealing with:
I have a ...
-2
votes
1
answer
172
views
Vanilla interface implementations. What should I call it? [closed]
Some may think I'm kidding, but I'm really stuck on this
Suppose you have some UserDao interface that you want to implement
What should you call it?
Here are a few points I'd like to make
I firmly ...
1
vote
2
answers
271
views
How modern runtimes handle both interpreted and JITted code at the same time?
I have a personal project, I want to write a JIT compiler/runtime in Rust (well, the language is not that relevant). I'm thinking about using a technique where the code is interpreted first and then ...
0
votes
2
answers
151
views
DDD: How to update a domain model with a lot of fields
Let's say there is a domain model:
@Table(name = "room")
@Getter
@FieldNameConstants
@AllArgsConstructor(onConstructor = @__({@PersistenceCreator}))
public class HotelRoom extends ...
1
vote
2
answers
236
views
In Java Interface contracts, does the @throws tag order should be considered?
Concrete example : https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/DataInput.html#readFully(byte[],int,int)
@throws NullPointerException if {@code b} is {@code null}.
is before
@...
-3
votes
2
answers
289
views
What to name a method which reads and sets value without return value?
I have a method like this
private void foo() {
this.myValue = readSomeValueFromFileSystem()
}
What is the good name for that ? Is there any convention about it ?
I feel that it is kinda set but ...
5
votes
3
answers
312
views
Hexagonal Architecture + Domain Driven Design. How to perform a correct implementation?
Currently, I am trying to implement these two architectures together with Java and Spring (although technology shouldn't matter I think).
But I'm encountering problems getting them to work together.
I ...
2
votes
4
answers
500
views
Why is ArrayList not a Stack
I've been wondering for a while why an ArrayList does not have a stack-like interface (think push() and pop())
I frequently find myself using these constructs in Java:
list.get(list.size() - 1);
list....
3
votes
4
answers
818
views
Are Optional fields okay?
Some fields may be optional. That is, a value doesn't have to be ever assigned to it. Luckily, Java has a special null-safer type for optional values — Optional
However, if I try to make a field of ...
2
votes
3
answers
315
views
Is there a way to avoid a lot of calls to other microservices from one single service request?
I'm developing a Microservices Web Chat Application using Spring boot and Websockets.
Right now my concern is the following: it seems like each one of my microservices need to make a lot of calls to ...
1
vote
1
answer
138
views
Best way to merge files in java [closed]
I have a task to merge set of files and each file contains words ( separated by new line ). Files contains words in sorted order. Once merged all the files, it needs to preserve the sorted order. ...
0
votes
3
answers
129
views
Java JPA: Transaction logic in DAO vs Service
Being new to JPA and transaction management, I have a Spring Boot REST API application layered like this:
Controller -> Service -> DAO -> DB
I want to be agnostic from any ORM and am using ...
-4
votes
3
answers
213
views
Java OOP problem
I'm working on a Java programming assignment. In this assignment, there's an immutable class Book and a mutable class BookCopy. You can have multiple BookCopy of the same Book.
Now here comes the ...
6
votes
6
answers
741
views
doSomethingIfCondition(). Is it good naming? [closed]
Suppose, I have some method that performs some job. In this example, it's going to be void, but it doesn't matter
public void doSomething() {
// doing something
}
But then, let's imagine, I got ...
1
vote
4
answers
402
views
Preventing payment to be processed twice
I have app with paymemt/subscription service, pretty much how the payment works is:
Initial payment
User initiate payment on the web ( click on "subscribe")
Back-End send init request to ...
-1
votes
2
answers
145
views
Rest Endpoint Design
I have 6 endpoints that return 6 json response:
/cities/{id} return a json object: {
"city": "Orlando",
"altitude": 10
}
/cities return an array: [{
"city": &...
2
votes
6
answers
913
views
Is it ok to assert on the behavior of return values of a testable class?
So I have a dialog for generating a random password. The user can set min, max, character categories (upper, lower, etc.). What the user basically does is configuring a StringGenerator that does the ...
0
votes
5
answers
360
views
Is it bad to pass builders as constructor arguments?
Note. It's a "spin-off" from my previous question. Not a duplicate — it focuses on a different topic
I got to know builders from Bloch's Effective Java. However, I made two changes to his ...
1
vote
3
answers
402
views
Should everything be buildable?
You have some class that performs a certain job
public class MyClass {
public MyClass() {
// ...
}
// ...
}
Then, a spec change comes around. The responsibility of the class is the ...
1
vote
2
answers
241
views
How should you test classes that access localized properties?
Suppose you have a class that accesses a property (perhaps, it's a GUI class)
How do you test it in Java?
You can inject (and mock) a ResourceBundle
import javax.swing.JLabel;
import java.util....
2
votes
2
answers
265
views
Is it ok to extend utilities?
Apache Commons has StringUtils. It's great, but I wish it had a shuffle() method or similar
Is it ok to create my own StringUtils that extends Apache's StringUtils and adds the method (Apache's class ...
8
votes
5
answers
4k
views
Expensive constructors. Should they exist? Should they be replaced?
Suppose I have a constructor that performs an expensive IO operation that takes a noticeable amount of time. I don't like it for a few reasons (first of all, it's simply wrong, but there are practical ...
0
votes
3
answers
142
views
Exposing dependencies results in "fat" constructor. What should you do next?
You take a non-testable class with a lot of static dependencies and expose, and expose until they are all explicitly declared in a constructor
But halfway through that nice plan, you notice your ...
2
votes
4
answers
279
views
Builder with non-defaultable required args
Suppose a class needs multiple properties of the same type. I don't want a big constructor where the user can mess up the order of args (remember, they are of the same type, no compile-time errors)
...
0
votes
1
answer
129
views
I'm trying to represent a Tree-like data structure, but running into OutOfMemoryError. Improvements? [closed]
Let me start by saying that I already found a solution to my problem, but I had to cut corners in a way that I didn't like. So, I am asking the larger community to understand the CORRECT way to do ...
1
vote
1
answer
124
views
Background thread processing vs queue based processing for relatively short tasks (max 30-40 seconds)
I need suggestion / recommendation on the design approaches mentioned below.
UseCase: I have a usecase where a client uses my system to generate some recommendations. Now, these recommendations are ...
5
votes
1
answer
287
views
What do you call an enum that translates its own values?
I see this pattern a lot, especially with countries, or more generally regions. An enum is defined with additional fields and with methods that translate to and from these values.
Example:
import ...
1
vote
2
answers
212
views
Abstraction for user notification
We have a desktop Swing application. It executes operations against a DB. It could be plain DML, it could be procedures. In both cases, it could result in an error. In that case, we need to display a ...
0
votes
2
answers
112
views
Where perform mapping in strict Domain-Driven Design?
I want to create an example application where we use a strict domain-driven design and layering (controller, service, repository). Most notably, we have a clear distinction between the domain and the ...
-1
votes
2
answers
112
views
Should uniqueness validation be on the database level or backend codebase level in data import
There is a kinda ETL task of importing data from csv to the database in project with legacy codebase and legacy database**. Data should be validated before persisting to database.
Validation includes ...
2
votes
3
answers
433
views
How do you square TDD with non-testable requirements?
I admire Bob Martin's Clean Code. Lately, however, I realized an apparent contradiction.
One of the three rules of TDD, which Bob Martin advocates for in his book, is to never write more code that is ...
-6
votes
1
answer
129
views
How to avoid NullPointerException(NPE) [closed]
Since we all know that few couple days ago there was a global outage (BSOD) because of the CrowdStrike updated which caused that global outage and the reason behind that was just an ...
-1
votes
1
answer
97
views
General Excel Processor and Validator
I've been working on this project where my responsibilities are to code for validating, processing and then dumping into database excel file.
Me and my colleague tried multiple approaches to make it ...
6
votes
2
answers
594
views
When the stack frames become computationally expensive
I've been experimenting with different data structures and algorithms in Python, Java and C to see in what circumstances function/method inlining could bring meaningful gains in terms of the execution ...
0
votes
1
answer
380
views
Global Variables State Management
Background:
I am working in a Java environment using Spring Boot, where I often encounter scenarios where global variable state management is critical, especially within singleton services. I have ...
-2
votes
1
answer
113
views
Spring/Java multiple shared modules for different databases
I'm implementing a software based on the micro-service architecture using Spring/Java. Each micro-service connects to either a PostgreSQL or a MongoDB database. Is it standard practice to have a ...
11
votes
7
answers
5k
views
How to maintain dependencies shared among microservices?
There is a dependency jar containing tons of service classes mostly used to retrieve data from database, and this jar is used among several different micro services in one cluster.
There is a big ...
1
vote
7
answers
467
views
How do I cleanly keep track of the type of various objects implementing a common interface without reflection?
In my multiplayer game I keep track of each player's inventory. I have a class for each inventory action that extends the abstract class InventoryItem. I then polymorphically call use() on the ...
2
votes
2
answers
248
views
What's the value in exposing the dependencies you used for your tests?
In Java, there's the fairly ubiquitous notion of a POM file which provides the dependencies of your modules.
For example
https://repo1.maven.org/maven2/org/typelevel/cats-core_2.13/2.12.0/cats-core_2....
6
votes
4
answers
1k
views
How to avoid init methods when 2 objects need the reference of each other?
According to https://softwareengineering.stackexchange.com/a/334994/432039, I know init is a code smell and should be avoided, and one of the solutions is to use a builder to hold the state first ...
2
votes
2
answers
190
views
Why does a Java lambda need to explicitly mention the method of the functional interface?
import java.util.function.Function;
public interface Printable {
public String print(String s);
public static void main(String[] args) {
Function<String, String> fn = p -> p +...
23
votes
9
answers
6k
views
Using commented dashes to divide up code chunks [closed]
I'm taking another crack at learning Java with the aim of getting a job. As I write code, I sometimes find it quite difficult to navigate my code using the formatting I often see in tutorials. ...
1
vote
2
answers
148
views
End2End/integration Testing in Java with Selenium - how to get a good test structure - looking for experiences [closed]
At work I am currently tasked to implement End2End/integration Tests for one application using Selenium.
we have an project consisting of a frontend and multiple backends (spring-boot apis). The ...
0
votes
2
answers
516
views
Should Value Objects be used inside the DTO?
After reading about Value Objects, I think they're pretty cool and should be used, but I am not sure if I am doing it the right way.
Let's assume that I have a simple DTO to create a user, which ...