The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
3
votes
1answer
245 views

Is Enum Polymorphism bad for DAO Methods?

I have been going back and forth in a discussion about polymorphic enums to call different DAO methods depending on enum entry, and I haven't been able to get a common agreement on this subject. Lets ...
1
vote
3answers
176 views

Which option is best, when you have an attribute that is a list?

I have a class containing a list, for example: class User{ .... List<String> cards ... } What's the best way to provide access to this member? List<String> getCards() or ...
-2
votes
0answers
36 views

Which pattern to use to do projection of POJO in API response?

Say that I have this POJO. public class Customer { public String id; public String name; public String lastname; public String birthdate; public String adress; } I have an API ...
0
votes
1answer
60 views

Better practice of workaround for implementing a type field in Java

We have a class in our company's main library as follows: public class User { private long id; private UserType type; // Getters, setters, constructors, etc. } and have several options ...
13
votes
2answers
88k views

Parameters are passed by value, but editing them will edit the actual object (like passed by reference)? [duplicate]

I've just discovered a huge hole in my Java knowledge. I knew that Java passes parameters by value. I thought I understand that and whenever I needed to edit the field object of a class, I create a ...
1
vote
1answer
34 views

RMI vs APIs vs Akka Cluster

We have a Java Play Framework Application which we want to scale out into a cluster. The only issue is that there are services ran by the Akka Scheduler once per day, and we don't want these services ...
2
votes
2answers
210 views

Domain models vs entity models vs Hibernate lazy loading

In a new Java project I plan to use an extra layer above Hibernate entities. So there would be a CustomerEntity and CustomerDomain. The reason for that is that the CustomerDomain can contain extra ...
3
votes
3answers
107 views

Should new fields and operation of logical entity be in one single class even the existing class has thousands of existing lines

In a legacy Java project, when adding a new feature - an existing email field can be editable or not editable on base of its parent system, a developer added a new feature by adding a new class, which ...
-3
votes
1answer
25 views

read text file with extended ascii from Google Cloud Storage (Java)

We have a text file with extended ascii (words like 'Systèmes' and 'Café'). It is in Google Cloud Storage. Its Content-Type is text/csv. If I download it via the browser UI (https://console.cloud....
3
votes
2answers
152 views

Is recommendable set as final as classes by default in a SDK?

I am working on the development of an SDK in Android with Java, In general the user doesn't need to inherit any class of the SDK, so the question is if it is a good practice put by default all classes ...
0
votes
2answers
68 views

Correct place to store semi dynamic data

What I mean by semi dynamic data are data that are expected to be changed only with product versions. Now imagine this scenario, In version 1 of the product, I have item status : SUCCESS , FAIL In ...
2
votes
2answers
216 views

Spring Boot REST Java Microservice: Why Use Maven Submodules?

I have seen a project which uses Maven submodules. The project itself is a spring boot application that exposes Restful API endpoints for a microservice ex: Customer Service (getCustomers, ...
-2
votes
1answer
530 views

How to avoid code duplication in this situation?

Case: Several methods with same params validation inside, like: public Response method1(...params...) { if (someCondition) { //do some stuff with side effects ... ...
4
votes
1answer
167 views

Delegate vs Forwarding in Java OOP

I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source: https://en.wikipedia.org/wiki/...
0
votes
0answers
44 views

Mapping properties to lambdas instead of using factory method/pattern

I'm receiving a JSON object which has many properties and the properties change (properties are added/removed) according to the type of the data in the JSON object. I'm expecting to receive many ...
0
votes
1answer
52 views

Generic crud with jdbc api

I am used to programming java on the server side with ORM frameworks and using abstract classes for CRUD. But now I'm workign on a desktop project without ORM and just the good old JDBC API for ...
1
vote
4answers
229 views

Do we always need to override equals/hashcode when crreating a new class?

When creating a new class, should we always override the equals and hashCode even if we don’t intent at that point to use the class with any Collection classes? Or is it better to wait till such a ...
38
votes
3answers
13k views

Why is the logical NOT operator in C-style languages “!” and not “~~”?

For binary operators we have both bitwise and logical operators: & bitwise AND | bitwise OR && logical AND || logical OR NOT (a unary operator) behaves differently though. There is ~ ...
3
votes
2answers
158 views

When covariance becomes an issue, how can I restructure my code and still be clean?

Suppose I have the following interfaces, GameObject and Enhance. GameObject: public interface GameObject { void prepare(); void use(); void cleanup(); } Enhance: public interface ...
2
votes
2answers
228 views

Why doesn't C++ support covariance in STL containers like C# or Java?

The Covariance and Contravariance feature is well supported in C# and Java collections. However C++ doesn't support them in their STL containers. Why is it so? For example the below code will ...
5
votes
3answers
502 views

Is it anti-pattern to have inheritence in a dto?

Are data transfer objects or POJOs meant to be final or can they be extended and create hierarchies for them? It is not clear to me if such a value class is properly designed only as a final class and ...
-1
votes
1answer
106 views

Making code more readable, iterating over inner map [closed]

So currently I looking for some hints from more experienced programmers. I am in the point where, when looking into my code it seems not readable, what is probably big flaw. So lets go to the code ...
2
votes
2answers
128 views

Field variable VS method variable

hey i have a difficult question. class DatabaseHelper { Database db; String defaultShema; public DatabaseHelper(Database db, String defaultSheme) { this.db = db; this....
10
votes
5answers
10k views

Is Java package level scope useful?

I understand the idea of package scope, and at times have even thought I wanted it. However, every time I set down with a serious intent to try using it I discovered it did not fit the needs I ...
0
votes
1answer
116 views

Do shared ViewModels contradict MVVM?

I want to follow the MVVM app architecture. I also need to have some communication between some fragments. But the official approach is pretty confusing. Taken from the docs (https://developer....
4
votes
1answer
396 views

How do I design concurrent scalable system with guaranteed ordering? How about Akka?

I have designed a simple PoC system that processes a feed of prices that tick. It consumes a stream of "Ticker, Price" objects off JMS and updates a map, so that the map simply contains the latest ...
0
votes
1answer
54 views

How to maintain and configure properties files for different environments in Java maven project?

A Java project with maven as build tool needs to be deployed to different environments, such as ci, dev, test, prod etc. Each environment has a properties file with the same set of properties such as: ...
1
vote
1answer
50 views

Java package naming conventions (in maven modular project)

Until now I have been using simple strategy for packages naming in my maven-modular projects. Each package name contains name of the module in which it is placed. For example, simple project would ...
0
votes
1answer
33 views

Design Java Testing class for hierarchical objects

Consider the following POJO structure in my main code. I want to create some testing framework for this kind of hierarchical classes, where the calling test method can specify if they want to modify a ...
2
votes
3answers
183 views

What is the proper logging level for tests?

I raised an issue at my employer concerning our gigantic heaps of testing logs. I raised the issue because I was spending well over a minute to find the relevant logs and stack-trace. I stated we ...
1
vote
1answer
61 views

Modeling objects that behave in different ways so that they can be stored in a collection?

Suppose I have a Player class with an Inventory: public abstract class Player { private final List<GameObject> gameObjects; public Player(){ this.gameObjects = new ArrayList&...
-3
votes
0answers
51 views

Design / style a code sequence of many checks on imput parameters, trying to avoid a long chain of “if … else if … else if”

In the entry point of a workflow (a REST service written in java) I need to make several checks to the entry parameters, and return specific error messages depending on what check failed (return on ...
12
votes
8answers
3k views

Object-Oriented Class Design

I was wondering about good object oriented class design. In particular, I have a hard time deciding between these options: static vs instance method method with no parameters or return value vs ...
41
votes
8answers
120k views

Why does java.util.ArrayList allow to add null?

I wonder why java.util.ArrayList allows to add null. Is there any case where I would want to add null to an ArrayList? I am asking this question because in a project we had a bug where some code was ...
1
vote
1answer
55 views

Abstracting common functionality for REST and CLI

I have made an application that deals with collections of images. Currently it has a REST api to add/remove images, create/clear/delete collections and a worker that can automatically fetch images ...
-1
votes
1answer
21 views

User list and roles integration

I work for a brand new start-up who wants to roll out a product. One of the task for me is to integrate the existing user and role details of the client company and integrate it with our company ...
104
votes
5answers
78k views

Excessive use “final” keyword in Java [duplicate]

While I understand what the final keyword is used for in the context of classes and methods as well as the intent of its use for in regards to variables; however, the project I just started working on ...
51
votes
11answers
10k 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
2answers
544 views

Method naming: to vs as vs get

Do the words to, as and get have any special meaning when used in method names? Examples to new Integer(4).toString() get Integer.getInteger("system.unknown.property",4) as Arrays.asList(...
-4
votes
0answers
39 views

how can i strip everything out without using regex

I have an XML that is being sent to an API endpoint to trigger a process in Dell Boomi. However, when I upload the file via a web page it ends up having the Content-Disposition and other stuff that ...
14
votes
5answers
4k views

Undefined behaviour in Java

I was reading this question on SO which discusses some common undefined behavior in C++, and I wondered: does Java also have undefined behaviour? If that is the case, then what are some common causes ...
24
votes
5answers
61k views

Best way to load application settings

A simple way to keep the settings of a Java application is represented by a text file with ".properties" extension containing the identifier of each setting associated with a specific value (this ...
1
vote
5answers
188 views

Design pattern for objects in invalid states

General design pattern for object error state Consider a simple class Wallet that models a wallet. A Wallet contains a certain amount of Wallet.Cash and it is possible to take money out / put money ...
-1
votes
1answer
39 views

Turning complex XML (or content tree) into HTML - How to do it (in Java)?

I have a deep object content tree that I transform into a XML using JAXB and a small self written API on top. But now I want to create a static HTML site that has basically the same content (and ...
-2
votes
1answer
44 views

How to safe Consistency in DDD project in single aplication?

I want to design my project in DDD spirit. This is single aplication (not microservices) but with separate BC (as modules). Each BC have separate mongoDB collection. I have problem with communication ...
3
votes
3answers
254 views

Why are test frameworks like JUnit or TestNG not more “object-oriented”?

By "more object-oriented", I mean, it appears to me testing frameworks like TestNG and JUnit could encourage testers to write implementations of Test and TestSuite interfaces. The current approach ...
1
vote
1answer
108 views

declare 2 classes for nullable / not nullable objects?

Let's say I have an which is loosely can be represented as: public class AnObject{ public AnObject(String name, String value, UUID id) { this.name = Objects.requireNonNull(...
-6
votes
1answer
285 views

What is the definition of an anti-pattern in the context of java 8 - Streams?

I'm reading this excellent series of articles about stream. Can a definition to anti-pattern in the context of java 8 -Stream will be a stream that does not produce a valid result using parrallel and ...
1
vote
1answer
158 views

Utils, Service class and Persistence Layer

I have a method in API handler which does API validation, performs business logic and then makes a call to db. Is it a good idea to move the business logic to utility class or service class? IMO the ...
1
vote
2answers
75 views

Creational design pattern that allows configuration of objects

With the factory pattern we abstract the creation of objects. But what if we need a specific configuration of an object that depends on the calling context? Example: So I have a Builder pattern for ...