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.

-2
votes
1answer
34 views

Managing the winning player in Mastermind [on hold]

I'm trying to implement a simulation of Mastermind in Java, for educational purposes. My version is slightly different from the original as more players can participate in a single match and try to ...
-2
votes
0answers
101 views

Why is unreachable code a compile error in java?

In java, unreachable code is considered a compile error. For example, this code would not compile: private static int double(int x) { return x * 2; x += 5; } As x += 5 can never be reached. ...
2
votes
1answer
133 views

Best pattern to solve problem where objects only differ in one attribute

As I am maintaining and extend a software system in Java, I saw a colleague (who left due to retirement) implementing a table with a generic approach. This approach is unluckily bound to tables (ui-...
-1
votes
3answers
60 views

How can I ensure only one instance of a micro-service queries a database and downloads files to shared storage at a time?

I have a micro-service that sends files to clients on request. It acquires these files by querying a database where other users have saved details of the file. The micro-service queries the database ...
-2
votes
0answers
39 views

What is the best hash function for Bloom filters? [on hold]

Bloom filters are used for pattern matching in Bioinformatics.Bloom filters are probabilistic data structures.So my question is what is the best hash function to use for bloom filters to get better ...
-3
votes
1answer
68 views

Should I not use inheritance when I need subtype cast/instantiation from basetype?

Problem BaseType has a field String jsonData that represents a JSON string. The structure of the JSON string differs according to a field String dataType. SubTypeA, SubTypeB, SubTypeC all extends ...
0
votes
2answers
118 views

Can I use a library that uses guice to bind contact and implementations in applications without issues?

I have a play application and want to take a common operation out from the application and make it as a library in order to use in other play applications. This proposing library has a contract(...
6
votes
1answer
223 views

Is the C# async/Task construct equivalent to Java's Executor/Future?

I'm a long time Java developer, but with so little traffic on SE, I don't limit my viewing to any single tags. I've noticed that C# questions with async/await come up a lot, and as far as I've read it'...
0
votes
1answer
470 views

Why did JUnit declare setUp and tearDown in camelcase, even though each of them is a single word?

Reference: http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/TestCase.html#setUp() setUp should have ideally be named as setup I've explored if any duplicate method setup is used in ...
-1
votes
0answers
51 views

How exception handling is related to object oriented programming? [duplicate]

As a programmer, I have write lines of code everyday using object oriented programming concept where I used each and every concepts of OOP and during which I also deal with exception handling in Java. ...
2
votes
2answers
193 views

Memory optimization of public methods in java

Does it optimize a program in java to declare methods private wherever they can be private or it doesn't change a thing and it's just a question of encapsulation? I was asking myself if declaring a ...
0
votes
1answer
94 views

When I add a new parameter to a java method, should it always be at the end of the parameters or are there other principles? [on hold]

What is the common practice of adding parameter to a method of a java class? Normally if I am modifying an equivalent method written in older python (without type support), I naturally put the new ...
1
vote
1answer
64 views

Let a subclass determine the implementation of its superclass field(s)?

Say I have two classes abstract class A, class B extends A, and I let A has a private field of type List<String> to indicate that every subclass has a field of type List<String>(so I don't ...
0
votes
2answers
90 views

What can I use as an alternative to protected interface methods in Java?

Occasionally, I want to write an interface that provides some complex method, and I want to make that a default method that uses some protected helper methods. The objective is such that I can later ...
12
votes
4answers
2k views

Which is the better way to call a method that is only available to one class that implements an interface but not the other one?

Basically I need to execute different actions given a certain condition. The existing code is written this way Base interface // DoSomething.java interface DoSomething { void letDoIt(String info)...
1
vote
1answer
108 views

Are JVM thread dumps a security concern?

When building parallelized applications using Java, a developer sometimes finds himself with a thread being blocked indefinitely because of a four-year-old bug in the spring-bean-web-rest-foo-bar-...
4
votes
1answer
163 views

Spring Boot - What is the purpose of Autowired Constructors?

I’ve been developing in Spring Boot for just over a year but have yet to understand the benefits of using an Autowired constructor and I was wondering if someone could explain this to me and what ...
1
vote
1answer
109 views

Contract based programming with verifications

I have a large project that is build contract based, where all functions verify input parameters and return values. But there are also cases where they for example verify that a call to a function is ...
1
vote
1answer
91 views

Is it okay to use a patch version in semver when updating documentation?

How does one go about e.g. missing documentation in the source files when using semver? I have just released 1.0 of my library and noticed that a class is missing JavaDoc - is it ok to release a PATCH ...
0
votes
0answers
12 views

JUnit testing with WSL Ubuntu 18.04 using Eclipse on Windows 10 [migrated]

Context In order to write integration tests that verify whether running a compiled project.jar file yields the desired results, I thought of doing: Execute a J-unit test that substitutes certain ...
1
vote
2answers
203 views

How to obey DRY principle

While trying to learn coding in Java and it's principles, I came across a problem with the DRY principle. From what I know, following a principle is not mandatory but if you should for many reasons. ...
2
votes
2answers
73 views

Query with a lot of fields and joins

I have to map a legacy database with tables that have a lot of fields and relations. For example (simplified code), @Entity @Table(name = "VISIT") public class VisitEntity { @Id private ...
-2
votes
3answers
291 views

Does Java Have True Single Inheritance? [closed]

I've been doing some studying on the types of inheritance. From what I've learned: Single inheritance refers to when a class inherits another class. Multi-level inheritance refers to when a class ...
0
votes
1answer
82 views

Do TCP Sockets need validity/security checks?

I am currently building a distributed system that consists of two separated nodes, connected over wireless Lan. The processes communicate over a bidirectional TCP Socket and JSON messages. For the ...
-3
votes
3answers
161 views

Try-catch in method or where the method is called? [closed]

I have the following method in Java: public method1() { //does something that might throw an exception } And then another method that calls the method above like so: public method2() { ...
3
votes
1answer
151 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
1answer
46 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, ...
0
votes
0answers
55 views

How to cope with terminal operations in a fluent API?

I am trying to polish my data-mapping library and struggle with my fluent API design as it feels clumsy in some basic use-cases. The library focuses on defining mapping objects from one type into ...
1
vote
4answers
214 views

Interface with similar methods

I'm currently taking over a project that has a common pattern of interfaces like this: public interface EmailService { void sendInvitationEmail(Payload payload); void sendNotificationEmailToAdmin(...
0
votes
4answers
297 views

Is Template design pattern a bad practice due to inheritance?

I have been using Template Design pattern in my code for implementing CRUD procedures for different resources. There are some steps which are same for many resources and some which need some addition/...
2
votes
2answers
99 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 ...
1
vote
2answers
72 views

How to allow initial input for a graph of words?

I want to create a java program that can find the shortest word ladder between two words. It's been suggested that I use a graph data structure with words as nodes. The idea being that nodes with ...
0
votes
1answer
33 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
3answers
189 views

Is it a good idea to use data structures(classes) provided by a dependency in our system?

I am building a new system, which I will call S, that needs to get some data from another system, which I'll name D. System D provides a client and a collection of classes(POJOs) to represent the ...
0
votes
1answer
51 views

Manage table relationships in Java dynamic web application that accesses a database, without using any framework

I'm developing a web application for academic purposes. What i have to do is a simple website which keeps track of realties along with their respective owners and the tags they are bound to. The ...
1
vote
2answers
154 views

What do you call a class that can be disabled [closed]

Is there a design pattern that fits this description? A factory that creates classes that can be disabled and enabled. The users of the class don't know if the functionality is enabled or disabled--...
0
votes
1answer
84 views

Class structure: How should a binary tree and it's node class be implemented in Java in terms of class files?

In implementing a binary tree in Java, should the node class be a separate class file independent of the BinaryTreeclass, or should it be a default class in the same class file as the BinaryTree class?...
5
votes
3answers
379 views

Java - Does an Interface really have no state or behavior?

going through the web I always read something like Java Interfaces have no state and no behavior If you look what the common definition of state is then you likely end with what the objects ...
2
votes
3answers
285 views

Is this design with globals bad?

I designed a Snakes and Ladders game. I divided into a reasonable amount of classes and I believe I did a good job regarding the OOP design. I have one question about a decision I made for the design,...
2
votes
2answers
143 views

destroy object once it won't be used anymore in flyweight pattern

I want to use flyweight pattern to return the same instance of my object each time. public class ThingFactory { private static final HashMap<UUID, Thing> things = new HashMap<>(); ...
15
votes
6answers
3k views

How to implement float hashing with approximate equality

Let's say we have the following Python class (the problem exists in Java just the same with equals and hashCode) class Temperature: def __init__(self, degrees): self.degrees = degrees ...
0
votes
0answers
33 views

Two JPA read/write Entities vs SpringJdbcTemplate for implementing CQRS without event sourcing?

So we are trying to implement DDD and CQRS (without event sourcing). I am need of clarity about the implementation details (Spring + JPA). DDD with multiple small aggregate roots require @Entity ...
0
votes
4answers
178 views

methods that has only behavior and not manage class state, is that violation of encapsulation

This is a simple scenario in an office with employees and their manager. In this scenario, both managers and employees have a name attribute. Only the manager can change his own name Only the ...
0
votes
2answers
64 views

What to test when testing an API? [closed]

When testing an API (with, for example, Java), what parts should I actually be testing when calling methods of my Controller class (e.g. a Spring RestController)? For example, lets say I've got a ...
1
vote
1answer
108 views

Why doesn't Comparable<T> include any type bound? [closed]

Why do you think the definition of Comparable<T> lacks an upper bound on T? That is, why is it not defined as: Comparable<T extends Comparable<?>> or Comparable<T extends ...
1
vote
0answers
61 views

Design Pattern to unify similar generated classes

I have a project where I work with generated classes for some web interfaces. Some of these classes are (almost) identical and most of the functionality I need them for only use the identical parts. ...
1
vote
1answer
56 views

Should I place constants related to reading a specific type of file in the FileReader class or in a separate class?

I am writing a class FileFoodReader (in java if that matters) that reads input from a file containing information about foods and their nutritional values. The file has to fulfill some requirements ...
5
votes
1answer
213 views

Why are the instance variables are declared as private in immutable pattern?

As the title says , Why are the instance variables are declared as private in immutable pattern ? I think that the keyword final is enough to avoid any changes that can be done to the value of the ...
1
vote
2answers
105 views

Object Oriented Approach to Calculations

I need to determine the average miles per hour for a trip and an object containing a list of trips. I'm thinking of two approaches. First approach, have a MphCalculator class that I would inject into ...
1
vote
1answer
124 views

Should an abstract class implement an interface, as opposed to defining its own abstract methods?

I'm defining a class structure for persisting to our cassandra database, and I'm unsure about using a combination of an abstract class and an interface. I have two concrete classes, one for persisting ...