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
3 answers
149 views

Communicating error conditions in client API for remote RESTful server, what's the best way?

I'm writing an application based on a RESTful API located in a server (written in Python). An Android app will access the API endpoints to get, post, put or delete data. The usual. The server has a ...
ismarlowe's user avatar
0 votes
1 answer
324 views

Should i specify that my methods "throws ConstraintViolationException" if the exception is actually thrown by a CDI interceptor?

Here is a sample method: @ApplicationScoped public class MyClass{ public void getUser(@Min(1) int id){ //get User logic } } I'm in a CDI environment with @ValidateOnExecution(type = ...
cidra's user avatar
  • 331
36 votes
3 answers
5k views

What is the point of repeatedly executing the same test?

I have recently learned about the not-well-known and not-widely-used annotation @RepeatedTest that, as the name implies, repeats the very same test n-times. Baeldung provides a short guide to this ...
Nikolas Charalambidis's user avatar
1 vote
1 answer
189 views

How to implement visitor if not all functions are supported for each Element?

I have two Visitor classes in my code Visitor1 and Visitor2 that execute functions on the classes ElementA and ElementB (both implement the interface Visitable that defines accept(Visitor)). I do not ...
sininen's user avatar
  • 129
1 vote
2 answers
405 views

Java - Is this a good use case of Java object optimization?

I recently joined a new project that is written in java. The project has been worked on for a few years by different developers, so there are many different code styles across different classes, and ...
user3334871's user avatar
-1 votes
1 answer
260 views

Building a function call tree at runtime

Edit #1 I believe that I misrepresented my intent when writing this question by focusing too much on the issues that have arisen from our misuse/misunderstanding of locks. I am making this edit to try ...
notphilphil's user avatar
-3 votes
1 answer
135 views

Using braces on new line convention, but should I do this for one-line methods? [closed]

I've been using the braces on a new line convention for my java project: public String class() { something; { However I have quite a few methods, like getters, that could be written on one line ...
Scott Adamson's user avatar
0 votes
1 answer
225 views

Excessively verbose and cryptic comparisons in Java

I don't know if this is the right place to ask more of a "philosophical" question. The more I code in Java, the more I have to bear with Comparable<T>. And the more I bear with this ...
Thomas Herondale's user avatar
-3 votes
1 answer
93 views

What is the correct collection to use in JAVA for indexing arrays of byte by a string in ordered way?

I am very new to the Java world (c# dev) and I have as asked the requirement to store some arrays of byte that should be indexed by a string and I have to keep the initial order of the collection. So ...
Dypso's user avatar
  • 231
0 votes
1 answer
36 views

Is there a set of guidelines on how to handle/code execution of Spring Microservices RESTful API calls?

I'm building a RESTful web api using Spring Microservices. I am following the Controller/Service/Repository Structure. Where can I find some guidelines on what the responsibility of each class should ...
Lucas T's user avatar
  • 753
-1 votes
2 answers
1k views

How to access version of a Java application programmatically when running from an IDE?

As far as I understand, the best practice for programmatic access to the version of a Java application is to: Specify version via build system (e.g. in Gradle). Include the version string as a ...
andrybak's user avatar
  • 113
0 votes
1 answer
94 views

Interface design for container that holds different instances derived from a common type

I'm currently designing an interface for a container that is supposed to store references of different instances that derived from a common supertype. An analogy of it would be as following: Suppose ...
torez233's user avatar
  • 101
3 votes
2 answers
600 views

Is it a code smell to have two different implementations of the builder design pattern, for the same model?

Shoutout to David Arno for teaching me about the builder design pattern via this thread! I have since used that pattern althroughout the code base to abstract out creating models from data stores, ...
Mike Warren's user avatar
1 vote
1 answer
2k views

How to remove unused code from a jar file? [closed]

I have a jar file, for example foo.jar. My code contains a lot of libraries (almost 75 jar dependencies). I am not using anything like maven or gradle, I'm just using pure java with pure jar files as ...
Day Trip's user avatar
0 votes
3 answers
544 views

Is it a code smell to have a static factory method on the base class?

Suppose we have a BaseModel, which has a type enum, and derived models with same constructor signatures to each other, whose implementations are like : public DerivedModelJ(Object arg1, ..., Object ...
Mike Warren's user avatar
3 votes
4 answers
696 views

Is it a bad practice to have an interface method to tell whether it can handle an object?

interface Resolver { boolean canResolve(SomeInput input); SomeOutput resolve(SomeInput input); } public static void main(String[] args) { List<Resolver> resolvers = ...; ...
Martin Tarjányi's user avatar
1 vote
4 answers
1k views

Are static classes/methods good for pure business logic?

I have a service class that performs some operations. One of the operations is a piece of code long enough to warrant extracting to a new class and unit test it in isolation: @Service public class ...
KidCrippler's user avatar
0 votes
2 answers
906 views

How to implement resources of a REST API as classes?

I need to access and process different resources from a REST API. For this I've come up with two different approaches: Approach 1: Create one class per resource, that handles accessing the data from ...
sininen's user avatar
  • 129
0 votes
1 answer
408 views

Efficient way to separate text file to header, tail lines and the leftover in between

I'm looking for a more efficient algorithm for the following problem: The input of the algorithm is a text file and two non-negative integer numbers - the number of headers and the number of tails. ...
kanbagoly's user avatar
  • 111
0 votes
2 answers
108 views

What is the (lowercase 't') true definition of "support" (to a computer scientist)? [closed]

tl;dr An audacious claim by a classmate caused me to question the definition of the word "support" (verb) in the context of computer science. Is it analogous to standard definitions where a ...
J_UPS's user avatar
  • 13
0 votes
1 answer
451 views

AWS Java Lambda Class Naming Convention?

I wonder if there is a naming convention or a best practice to name Java Classes when building AWS Lambda functions with Java for REST API? For example, if I have a Java project that contains Lambda ...
InterestedDev's user avatar
1 vote
2 answers
400 views

How to avoid unit test duplication in wrapper classes?

Assuming I’ve a class with three methods, startCollection, add, endCollection. One test case is: if start was not called, add should return an error. I’ve mocked the persistency storing the state with ...
user avatar
8 votes
6 answers
5k views

How should an API handle unsupported fields?

Let's assume I have this API on /api/v2/persons that enables me to create new entries by POSTing this JSON: { "name": "me" } The API is implemented using Spring Boot and if ...
Marged's user avatar
  • 225
2 votes
3 answers
270 views

Exposing multiple classes from the representive package for one entity

I have a simple app for storing the movies, that exposes REST API. I am using spring-boot. I am using this simple app as an example, however, the question is more about general good practice. I am ...
Darlyn's user avatar
  • 209
6 votes
5 answers
5k views

Assertion statements: Remove or retain in production code

I'm working in a large company and am doing my first Java project here. We have certain code style guidelines. In the Java guideline, it is said that "assertions are only intended for debugging ...
Green绿色's user avatar
-2 votes
1 answer
166 views

Version naming for source code with multiple build targets

I have a Java library which is deployed automatically to Maven central. Right now this library has Java-1.8 target SDK, I want to continue releasing source code with this target and add new target ...
g4s8's user avatar
  • 126
9 votes
2 answers
1k views

Should function names describe their parameter types?

If you wish to perform the same action using different parameters, you can either make differently named functions: public Apple findAppleById(long id){ return repo.findById(id); } public Apple ...
Bar Akiva's user avatar
  • 217
-1 votes
1 answer
278 views

How to create, manage and use multiple dynamic database connections at run time?

As I am creating different database connections on the basis of database credentials (i.e. JSON or POJO with all the credentials required to create a DB connection). e.g. Imagine a Map containing ...
Amber Pandey's user avatar
0 votes
1 answer
88 views

Interactive and Batch traffic in one service [closed]

I am designing a workflow, and am trying to avoid parallel deployments of the same service. Thus I am looking to have one service that handles both interactive and batch traffic. My main concern is ...
Brian's user avatar
  • 109
-2 votes
1 answer
869 views

How to implement state machine to card games such as poker or russian poker?

These are my class designs: I also have Controllers where I can create table, create users add users to table, deal hands simulate user bets using PostMan (Above classes are game engine classes, I ...
Sunuba's user avatar
  • 21
1 vote
4 answers
2k views

Does the JVM compile bytecode into machine code and run it or does it just run bytecode directly?

Java source code is compiled into bytecode for the JVM. But, how does JVM convert bytecode into machine code? Does it re-compile bytecode into machine code and then run it? Or does it simply just run ...
Grateful's user avatar
  • 149
-1 votes
1 answer
791 views

How to combine data from multiple sources into the same object?

I've been running into a common pattern when requesting data from multiple sources: Have a list of objects from one source (e.g. a list of Cars) with an id property and a few other properties ...
user8297969's user avatar
-1 votes
2 answers
208 views

Implementation of method differs only in one line

I have 2 implementation of the interface: public interface MyInterface{ void getCollectedData(MyData mydata); } public class MyImpl implements MyInterface{ public void getCollectedData(MyData ...
Johnyb's user avatar
  • 407
0 votes
2 answers
270 views

Is it bad to use checked exceptions as API response?

Consider the following code: public class User { private String password; public void changePassword( String oldPassword, String newPassword) throws ...
George Z.'s user avatar
  • 705
0 votes
1 answer
155 views

Exception handling with failure atomicity in desktop applications

When it comes to exception handling, there are many guidelines and best practices on the web. On of them is to throw early, catch late, or even Don't Catch. So when facing an exception, the current ...
mperktold's user avatar
  • 151
1 vote
2 answers
260 views

Granularity of a Method

I have a general design question. Suppose I have a List: List<String> list = new ArrayList<>(); list.add("Str 1"); list.add("Str 2"); list.add("Str 3"); I ...
gaout5's user avatar
  • 41
1 vote
2 answers
726 views

Concept/Design question: Alternatives to switch/conditional statements and Enums

I am practicing design patterns and OO concepts such as inheritance in java and I'm writing an application that represents a vending machine. I have two questions focused on ideal structure and design ...
Bob Jones's user avatar
6 votes
9 answers
2k views

Java instanceof and Clean architecture

It seems to me that there is a conflict between clean architecture and the recommendation not to use instanceof. Consider the following code: class ParentEntity { } class AEntity extends ParentEntity ...
Shayan's user avatar
  • 199
0 votes
0 answers
623 views

Why does Protobuf generate outer classes for Java?

I googled this and found questions like "How to use Protobuf message as java class without a java outer class?" (https://stackoverflow.com/questions/60312156/how-to-use-protobuf-message-as-...
Matt Welke's user avatar
1 vote
2 answers
378 views

Are experienced developers and software architects able to describe an entire software application in terms of design patterns?

Do experienced developers and software architects see entire application in terms of design patterns? In other words experienced developers and software architects able to describe an entire software ...
user2330678's user avatar
0 votes
1 answer
115 views

Should I define a separate interface for clarity rather than implementing Supplier<T>?

Let's say you have to meet some prerequisites before doing something. For that, I think implementing Supplier<Boolean> is a great candidate since it's widely used, and quick to understand. But, ...
eca2ed291a2f572f66f4a5fcf57511's user avatar
2 votes
3 answers
161 views

Java design: there is two interface: B extends A. A and B have one subclass each, named ABase and BBase, can I make BBase extend ABase?

there is two interfaces A and B: public interface A { } public interface B extends A{ } A and B have one subclass each: public abstract class ABase implements A{ } public abstract class BBase ...
Guo's user avatar
  • 177
1 vote
2 answers
344 views

Is using KDoc/Javadoc comments inside of a function considered bad practice?

I often find it helpful to use KDoc/Javadoc comments inside of a class or function instead of normal comments. IntelliJ colorizes them more obviously by default and, more importantly, allows ...
Matt Robertson's user avatar
0 votes
2 answers
368 views

Difference between Efferent Coupling and Dependency Injection

I am working on research that analyzes dependency injection (DI) in Java projects. The more I read, the more I get confused by DI in relation to other frameworks and even software quality metrics. I ...
Narnian12's user avatar
1 vote
1 answer
2k views

How do you pass enums between microservices, without need to have copies of enums in different services?

I'm working with microservices, and I see an issue. There are two services, that transfer info between each other via jsons. Because of this, we have to store our enums in strings, and serialise-...
keldranase's user avatar
2 votes
4 answers
2k views

Alternative to using enum

The project I'm working on has a code dependency on a TeamNames enum. The problem with this is that the project needs to be recompiled and redeployed on any addition/deletion in TeamNames. How can I ...
NiceOne's user avatar
  • 23
3 votes
2 answers
1k views

Should "add" methods return the update object? [Java - Clean Code & Best practices]

I'm following a course on Udemy and the teacher said that is a best practice return the update object in an add method. This is the example: public Recipe addIngredient(Ingredient ingredient) { ...
SGiux's user avatar
  • 191
-2 votes
1 answer
261 views

Why aren't constructors atomic? [closed]

If thrown exceptions in constructors can lead to memory leaks or partially-constructed objects, then why we don't make them atomic? so neither an object nor it's local variables will get created/...
Rain's user avatar
  • 99
1 vote
1 answer
197 views

Wrapping methods without Decorator Pattern or AOP

I have a Dao interface which doesn't have any methods defined. Then I have multiple classes which implement this interface, like PetDao and HouseDao. For some of the implementation classes, and some ...
Pepit's user avatar
  • 13
-1 votes
1 answer
2k views

Locking a DB table and waiting for writing/reading it from a web application

I'm working on a web application using Spring (Java and JPA + Hibernate) and I was wondering if there is a way of locking a MYSQL table and then when another web service (or even another thread from ...
Jordi Pagès's user avatar

1 2 3
4
5
99