Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
70 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 ...
1 vote
1 answer
9k views

use of minOccurs="0" without nillable="true" in SOAP

I have received an XSD from a 3rd party supplier generated from a Java based system; which is to be used to create a SOAP endpoint for us to receive data transfers. Their XSD does not make any use of ...
0 votes
3 answers
253 views

How best to structure my Service/Repository layers when persisting a Many to One object?

I'm working on a project where I need to do CRUD operations on Book and Library objects. Naturally the relationship between Book and Library is Many to One, like so: @Entity @Getter @Setter @...
149 votes
15 answers
31k views

Why did memory-managed languages like Java, Javascript, and C# retain the `new` keyword?

The new keyword in languages like Java, Javascript, and C# creates a new instance of a class. This syntax seems to have been inherited from C++, where new is used specifically to allocate a new ...
5 votes
5 answers
3k views

Allowing users to add their own custom fields in a Spring MVC Hibernate application - What's an ideal approach?

We all may have seen applications like JIRA, or many CRM or other applications that allow its users to define their own custom fields to an entity, and do a variety of stuff with it, like making them ...
-2 votes
1 answer
70 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 ...
16 votes
7 answers
43k views

What is the better way to escape from too many if/else-if from the following code snippet?

I am trying to write a servlet which does task based on the "action" value passed it to as input. Here is the sample of which public class SampleClass extends HttpServlet { public static void ...
6 votes
2 answers
503 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 ...
1 vote
3 answers
1k views

How to design an API wrapper with bulky operations on domain object? (Need guidance)

I need some guidance in designing an API wrapper for my backend APIs. I have tried to keep it as specific as possible. Context: We have a project which supports certain file operations like edit, ...
1 vote
7 answers
442 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 ...
0 votes
0 answers
126 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 ...
1 vote
2 answers
441 views

How to design a process and use the business rules for sending alerts/notifications

Currently working on designing a process which demands me to send alerts like email notifications to the users which meet a business criteria(we can also call business rules). I want to make this ...
7 votes
2 answers
2k views

Does this factory method pattern example violate the OCP?

In Head-First Design Patterns, they use a pizza shop example to demonstrate the factory method pattern. public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; ...
1 vote
1 answer
754 views

Open closed principle vs abstraction leaking (Java enums)

In Java, an enum is not a plain replacement for a number (like in C/C++), but a family of objects which can have properties. For instance public enum Order { NAME("Ordering by name"), SURNAME("...
38 votes
8 answers
23k views

Is overriding Object.finalize() really bad?

The main two arguments against overriding Object.finalize() is that: You don't get to decide when it's called. It may not get called at all. If I understand this correctly, I don't think those are ...
8 votes
5 answers
2k views

Open Closed principle in design patterns

I am bit confused about how Open Closed principle can be applied in real life. Requirement in any business changes over the time. According to Open-Closed principle you should extend the class instead ...
6 votes
6 answers
824 views

In creating a "registry", which is worse: using reflection or violating open/closed principle?

In my current software engineering course, my team is working on a library management system that is essentially a command-line/REPL environment with a half dozen commands, e.g. checkout, search, etc. ...
6 votes
5 answers
1k views

How to refactor this code to obey the ‘open-closed’ principle?

The UML is listed below. There are different products with different preferential strategies. After adding these products into the shopping cart, the caller needs to call the checkout() method to ...
28 votes
9 answers
7k views

Coupling: Theory vs Reality

Coupling is defined as the knowledge one object has about another one, which describes how dependent they are. The more dependent, the worse, since changes in one would impact in the second. High ...
11 votes
7 answers
4k 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
4 answers
3k views

Multithreaded processing of single REST requests

Background: We're providing an API that provides information about all users within a given group. Our API is a high level facade over another low-level REST API. To gather those information we first ...
2 votes
2 answers
235 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....
10 votes
1 answer
540 views

Should I be concerned with dependencies?

Our organization uses IBM ClearCase to manage its versioning controls (for better or worse). We've been working on our application for several years now, and a large number of activities have started ...
5 votes
2 answers
17k views

How to calculate percentile in Java without using Library

I am trying to calculate 95th Percentile from the data sets which I have populated in my below ConcurrentHashMap. I am interested in finding out how many calls came back in 95th percentile of time My ...
9 votes
3 answers
11k views

Understanding the need of Visitor Pattern

After seeing an article on visitor pattern, it is clear to me how it works. And I created a sample program for my understanding; main(){ SortingAlgorithm bubbleSort; : intList.sort(...
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 ...
3 votes
4 answers
378 views

throwing an exception or returning Optional

In a service daemon coded in Java, we have services for getting various objects. When we use these services forgetting an object, I wonder if the method should return the object and throw an ...
4 votes
6 answers
2k views

The ID of an object is null at start. Does that make the object state invalid and violate encapsulation?

This question is related to How should an `Employee` class be designed? In the above question, to uniquely identify an employee, each Employee object has an id field as shown below class Employee { ...
4 votes
6 answers
3k views

Code smell java:S115 - Constant names should comply with a naming convention

In my Java 11 / Spring Boot codebase I have enums that don't respect the convention to have all uppercase constants because when we move to frontend the data there has to be camelCase (or even random /...
5 votes
1 answer
11k views

Forward Slash vs Back Slash vs File.separator in Java

I've seen some programmers doing the following : File file = new File("folder\\subfolder\\subsubfolder"); And I find it totally wrong because of compatibility issues with a different OS than ...
2 votes
2 answers
174 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 +...
22 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 votes
1 answer
3k views

If I have 2 different implementations of the same interface but with the same methods should I have 2 interfaces? [closed]

I have 2 different implementations of the same interface, but each one of those objects have different implementations, however they have the same methods, should I create one interface for each type ...
2 votes
2 answers
1k views

Passing a list of base class objects to a method and using instanceof to filter them

I'm working on a Java reverse engineering project, where I am analyzing bytecode and trying to identify classes, methods and fields using the ASM framework. After identifying these I transform some ...
5 votes
1 answer
629 views

Applying Domain Driven Design - Model/Implementation

I'm quite new in the DDD-World and I'm just trying to figure out all the basics so please bear with me! I have the following Entities: - Datamodel - Object Types - Object Fields A datamodel can ...
1 vote
2 answers
80 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
201 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 ...
23 votes
5 answers
16k 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 ...
19 votes
4 answers
6k views

The suffix Exception on exceptions in java

Specifying a suffix of Exception on exception classes feels like a code smell to me (Redundant information - the rest of the name implies an error state and it inherits from Exception). However, it ...
2 votes
0 answers
405 views

Examples for "serverless" Java service for AWS Lambda

Doesn anybody have practical experience with implementing a (micro)service based on AWS Lambda? My team is currently working on an MVP. We started with a monolith and that has worked very well. It's ...
2 votes
2 answers
344 views

Should mocks used in unit tests represent entireties or subsets?

I was working on a unit test that has a mock object looking something like: public class TestMock { static { Doodad a = new Doodad(0); Doodad b = new Doodad(1); Doodad c = ...
49 votes
6 answers
8k views

What would be the disadvantage to defining a class as a subclass of a list of itself?

In a recent project of mine, I defined a class with the following header: public class Node extends ArrayList<Node> { ... } However, after discussing with my CS professor, he stated that ...
0 votes
1 answer
113 views

Where to put getOrSave responsibility?

I have many repeated parts of service logic which just fetches object if it exists or returns a newly saved one. I want to move it from service because it just clutters up the logic. But I do not ...
-1 votes
4 answers
294 views

Is setting a flag in a loop a code smell?

I have a loop that I am testing a condition in but if the condition is not met then after the loop is complete, I want to execute another code block: Boolean loopConditionNotMetFlag = true; for (List&...
9 votes
5 answers
5k 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....
6 votes
2 answers
3k views

Trouble with circular dependency in state machine design

I am trying to develop the structure for a basic state machine that can also take in input and produce output. I've hit a bit of a mental block in trying to figure out how to model the relationship ...
1 vote
5 answers
185 views

End method in normal flow versus exception flow

Consider the two following examples: public Something fetchSomething(String key) { if(somethingsMap.containsKey(key)) { return somethingsMap.get(key); } throw new ...
4 votes
4 answers
700 views

How to do "Separation of concerns"

I understood(edit: I assume) the importance of seperation of concerns and benifits in an application, But struggling to identify what are considered to be a concern (developer, feature, consumer or ...
0 votes
1 answer
205 views

Should a Java project use UTF-16? [closed]

Java, by default, uses UTF-16 to represent characters in the String data type. I inherited a JavaFX project which currently has some Strings in UTF-8 and others in UTF-16. This is causing bugs (in pop-...
-1 votes
2 answers
193 views

Idempotency for a financial transaction API

Say you have a REST API endpoint like POST /move-money which transfers money from your main account to a savings pot. There are three path parameters accountId for the user's account potId for the ...

1
2 3 4 5
100