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.
5,020 questions
1
vote
4
answers
170
views
Is it good practice to return ApiResponse from a Spring service layer?
I'm working on a Spring Boot application with a VoucherService and VoucherController. I currently have the service method return an ApiResponse<T> directly, like this:
@Transactional
public ...
2
votes
3
answers
660
views
Convenience inheritance
Sometimes, you inherit from a class that defines the semantics of your type (Shape – Ellipse – Circle). Other times, you inherit simply because it's convenient to do so.
A superclass may have ...
7
votes
3
answers
551
views
What SOLID principles does the Java List interface challenge?
The Java List<E> interface includes methods, such as add(E) and remove(Object), that work as the names suggest on instances of mutable concrete implementations, such as ArrayList<E> and ...
1
vote
1
answer
236
views
What is the difference between Vertical Slice Architecture and Feature-Based Architecture
I am refactoring my monolithic application, in which the code is organized based on layered architecture.
I want to implement Modulith (Modular Monolit) in my app, but I've run into a problem: I haven'...
1
vote
2
answers
260
views
Testing GUI panel involving modal dialogs
Suppose you have a panel with a table, which I will call a pane.
The table has a toolbar above it, including an edit button.
Editing involves showing an editing dialog. It allows the user to edit the ...
0
votes
2
answers
204
views
Keeping the impact of changes low when moving shared domain objects into a library
I’m refactoring a microservice project where multiple services share the same domain objects. Over time, these objects have diverged across services, causing inconsistencies.
To solve this, I plan to ...
0
votes
2
answers
146
views
A class that implements two interfaces that extend the same interface [duplicate]
I'll get straight to the point. I want to implement a class structure in Java similar to the one in the image.
Am I falling into bad practice with this kind of diamond-shaped interface implementation?...
21
votes
5
answers
5k
views
Why should I never ever ever use Java serialization?
I've heard that I should never use Java serialization (Serializable/ObjectInputStream/ObjectOutputStream) because of security. What's the problem?
23
votes
9
answers
7k
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. ...
8
votes
7
answers
5k
views
Must getters return values as is?
We have entities with numeric properties, they are of boxed types (e.g. Integer). Typically, each of those properties has a pair of getters: getInt(), which returns zero on null values, and ...
4
votes
2
answers
330
views
Managing Growth in Microservice Architecture: Is Modular Monolith the Solution?
Our team of 5 members are managing a microservice architecture that currently includes around 200 Java Spring boot microservices, with approximately 50 new services being added each year. We follow ...
3
votes
2
answers
512
views
Why do I need an authorisation server if my micro services can validate JWTs directly?
I'm working on a Spring-based micro service project and considering different approaches for handling authentication and authorization. Instead of setting up a dedicated authorization server, I'm ...
1
vote
5
answers
561
views
Code reusability/inheritance introduces pointless testing
Say I have a bunch of classes that imitate cars: SportsCar, Truck, and SUV. All of these classes share some public methods like start() and stop() which they inherit from an abstract class Car. While ...
2
votes
3
answers
271
views
Search requests with multiple search values
(related: Fetching records matching multiple joined attributes)
If Spring Data doesn't allow GET requests to have a body (and it's considered bad practice anyway)
curl -X 'GET' \
'http://localhost:...
0
votes
3
answers
221
views
Tracking data change in GUI form
Problem: our application allows users to close a form window after certain changes without any confirmation, instead of pressing the Save button. This makes them complain they have to start afresh.
We ...
-1
votes
1
answer
328
views
Pattern for a chain of unreliable steps
I would like to refactor some code of a process that must perform a processing. The processing involves several steps, each of which can fail or go well. If successful, it must proceed to the next ...
389
votes
35
answers
640k
views
Why isn't Java used for modern web application development? [closed]
As a professional Java programmer, I've been trying to understand - why the hate toward Java for modern web applications?
I've noticed a trend that out of modern day web startups, a relatively small ...
5
votes
5
answers
977
views
Handling class specific behavior: polymorphism vs instanceof
I'm designing a system with different types of components, and I'm trying to decide whether to use polymorphism or instanceof checks for handling type-specific behavior.
I see two possible approaches:
...
1
vote
2
answers
208
views
Classpath hell: independently updated apps with shared dependencies
This is how our desktop applications are launched. In this case, it's called DesktopApp (I changed the actual name). As you see, the dependencies are hard-coded, including their versions. In the ...
3
votes
4
answers
2k
views
Avoiding instanceofs with GUI composites
I have a stack of custom Row widgets. Each of them is a label with some editComponent (often a JTextComponent). Rows are aware of their previous and next siblings (if a Row has no such sibling, the ...
311
votes
14
answers
485k
views
How do you unit test private methods?
I am working on a java project. I am new to unit testing. What is the best way to unit test private methods in java classes?
277
votes
14
answers
126k
views
Should we avoid object creation in Java?
I was told by a colleague that in Java object creation is the most expensive operation you could perform. So I can only conclude to create as few objects as possible.
This seems somewhat to defeat ...
4
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 ...
4
votes
4
answers
462
views
Documenting properties
Suppose I have a property with an associated getter and setter (the property is not exposed directly)
Where should I describe the property?
The field javadoc
The getter javadoc
The setter javadoc
...
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 ...
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 ...
0
votes
2
answers
160
views
Is it better to pass a specific “context” object to handlers rather than the entire domain object?
I’m designing a system where various “handlers” apply business rules to an object. Initially I had each handler receive the full domain object:
// A large domain object with many properties and ...
2
votes
3
answers
451
views
Naming convention for boolean returning methods
There's a convention for boolean property accessors to name them like isX().
Does it apply to all boolean returning methods that are not invoked for their useful side effects, like this one?
Also, ...
296
votes
16
answers
30k
views
Grokking Java culture - why are things so heavy? What does it optimize for? [closed]
I used to code in Python a lot. Now, for work reasons, I code in Java. The projects I do are rather small, and possibly Python would work better, but there are valid non-engineering reasons to use ...
0
votes
1
answer
132
views
How to create a job management for a hybrid cloud/on-premise software?
I am exploring the state-of-the-art methods to create a service that can run and scale in both cloud (container) and on-premise environments.
The current version of the software is designed for on-...
0
votes
1
answer
222
views
How should ViewModels and Services communicate in a JavaFX + Spring Boot MVVM application?
I'm building a JavaFX + Spring Boot application using the MVVM pattern.
I’m building a generic “wizard” in a JavaFX + Spring Boot MVVM app. A WizardViewModel drives a sequence of steps (STEP_ONE → ...
9
votes
5
answers
6k
views
How can an interpreter run code without translating into machine code?
I'm in my intro classes and trying to understand how a Java compiler works. Most posts said a compiler translates A to B (which could be machine code) to run, while an interpreter 'just' runs the code....
119
votes
12
answers
20k
views
Rationale to prefer local variables over instance variables?
The codebase I'm working on frequently uses instance variables to share data between various trivial methods. The original developer is adamant that this adheres to the best practices stated in the ...
2
votes
2
answers
251
views
Best way to add asynchronous execution for already implemented synchronous process
I have a complex process implemented in Java Spring microservice.
Currently this process is triggered on user request and it is synchronously executed.
This often results in a gateway timeout.
...
19
votes
6
answers
9k
views
Is utilizing a singleton for a cache an antipattern?
I'm currently writing an MVC application and I need a class that can:
A: get, add and remove data(specifically a TreeSet of sorted strings that I want stored in memory, but I doubt the data itself is ...
5
votes
4
answers
353
views
Ugly nested parameterizations
Suppose there's a long IO operation that may result in some return value — but doesn't have to. Completion with no return value is different from the operation being pending.
Do I have better options ...
106
votes
11
answers
17k
views
Has / can anyone challenge Uncle Bob on his love of removing "useless braces"?
I hate referencing paywalled content, but this video shows exactly what I'm talking about. Precisely 12 minutes in Robert Martin looks at this:
And says "One of my favorite things to do is getting rid ...
23
votes
5
answers
18k
views
Are "factory methods" anti-pattern now?
I consider Joshua Bloch's Effective Java the best book on the language that I read. However, I started to wonder about something
One of the things he suggested was to prefer public static factory ...
7
votes
9
answers
8k
views
Why just "interface segregation principle" but not "method segregation principle"?
According to one definition of "interface segregation principle" that states currently in Two contradicting definitions of Interface Segregation Principle – which one is correct?, a client ...
61
votes
11
answers
15k
views
Why does C++ have 'undefined behaviour' (UB) and other languages like C# or Java don't?
This Stack Overflow post lists a fairly comprehensive list of situations where the C/C++ language specification declares as to be 'undefined behaviour'. However, I want to understand why other modern ...
3
votes
3
answers
554
views
How to handle checked exceptions on interface contract in Java
I am starting a project in Java and ran into the following situation. The application requires a persistence layer for a certain document type. This could be a MySql database, an AWS Dynamo DB ...
1
vote
4
answers
211
views
Ensuring type safety, honoring supertype contract
Due to type erasure, I can't do instanceof T in Java. With that in mind, how do (should) I write, for example, a generic TreeNode (or, more precisely, DefaultMutableTreeNode)? setUserObject() accepts ...
0
votes
1
answer
116
views
Designing a Scalable Caching Layer for User and Tenant Metadata in a Messaging System
I'm developing a microservice-based application that processes a high volume of messages. Each message must be handled according to the user’s personal settings and some tenant-specific (customer) ...
196
votes
40
answers
21k
views
My Dad is impatient with the pace of my learning to program. What do I do? [closed]
So my Dad bought me 5 books on programming (C++, Java, PHP, Javascript, Android) about a month ago. He's an architect and he knows NOTHING about programming. He bought me them because I told him ...
2
votes
3
answers
182
views
Domain data classes with fluid property lists
Is it fine for your data models to have a sort of "open-ended" list of properties?
For example, you may have some Employee:
// getters, setters, annotations are omitted
public class Employee ...
14
votes
5
answers
5k
views
How to "Tell, don't ask" when 2 objects involves in the condition and the decision at the same time?
According to Explanation on how "Tell, Don't Ask" is considered good OO, I know the following is bad:
if(a.isX){
a.doY();
}
public class A{
public boolean isX;
public void ...
1
vote
2
answers
252
views
Is Spring Boot Unit Test Coverage with Integration tests only a bad practice?
I have recently come across a few codebases at work where the previous developers chose to reach the >80% coverage criteria by writing only integration tests with the @SpringBootTest annotation ...
2
votes
3
answers
1k
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 ...
6
votes
5
answers
841
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 ...
4
votes
3
answers
909
views
Clearing static state before testing each method
My SUT class depends on external static state (which generally should be avoided, I know).
How do I make sure my tests do not depend on their order of execution?
I mean other by introducing some reset(...