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
-2
votes
2answers
86 views

How to have zero or one of and object in a class? [closed]

I want a class A, and I want the relation that A can have zero or one X. So I wrote this class: public class A { private X x = null; public A() {} public A(X x) { this.x = x; } ...
2
votes
2answers
90 views

A good way to represent an object with multiple interconnected states?

I want to program a simple budget application. In my application, the budget is divided into budget periods (months by default, but it can be bi-weekly or weekly periods). In each period there is a ...
1
vote
1answer
97 views

Best Way to Store List as Constant

Let's say I want to parse some JSON and I store the path in a Java constants class the path that I want to follow. For example: public static final List<String> path = Arrays.asList("a", "b",...
2
votes
1answer
166 views

Is it efficient and a normal practice to have one class with thousands of lines of code? [duplicate]

I am currently in a continuous software product development project on base of legacy Java code. The source code is very complicated, which is good and bad. But I am surprised to see that in the core ...
-1
votes
1answer
137 views

Synchronization method vs private final object for locking in java

Can anyone tell me which one is better approach between synchronization method and private final object in java and why ?? public class kclass { private final Object myObject = new Object(); ...
38
votes
8answers
6k views

How long to wait before deleting a deprecated method? [closed]

I am maintaining a public API and have to deprecate a method. Is there a general rule on how many months/years/versions before the deletion I should deprecate a method?
1
vote
1answer
938 views

Using generics on interfaces when implementation is not generic

Is it acceptable practice to put generics on an interface when the implementation will not be generic? Lets say that my project will have many classes that read data from the database. I may make a ...
-2
votes
1answer
57 views

Understanding property based testing

I'm reading about property based testing and I'm wondering how can I test this my code using that paradigm. class Invoice { private final String id; private final String companyName; ...
2
votes
0answers
52 views

java hashtable extending to support duplicates

I have to maintain an old application and have to extend some modules. There is an hashtable that is used for maintaining/holding some objects as representation of running (real) processes (don't see ...
0
votes
1answer
41 views

Reuse JSE checked exceptions as unchecked

Actually what i'm doing in my code base is duplicating already existing java checked exceptions like MalformedURLException.class by keeping the same exception name while inheriting from ...
2
votes
1answer
354 views

Is it a good idea to have type safe equals

To me, equals() between objects of inconvertible types is one of the most useful IntelliJ inspection. It's almost never intended to have objects with different types equal to each other. I ...
1
vote
1answer
156 views

Where and how to connect to external API in my service class?

In my service class I would like to connect to external API. Where and how should I do it? 1) Inject in constructor ExternalClass and assign to private property. Next in other property in constructor ...
4
votes
1answer
447 views

Wrapping utility classes and injecting them for unit testing purposes

I found that it is so hard to test classes that depend on other utility classes as java.nio.file.Files. It is also impossible to mock them using the classic unit testing stack (junit,mockito,..) ...
3
votes
3answers
538 views

Are there any drawbacks to using a nested class instead of declaring a new one?

I'm doing code review on a change my co-worker made to our Java application, and I've found something I'm not very familiar with - a nested class. From reviewing the code, it seems like the nested ...
3
votes
1answer
150 views

Java inheritance error handling scenario

I looked multiple places for some advice on how to solve this before asking this question, but I couldn't find something similar to this. So I have the following scenario in Java Spring Integration ...
2
votes
4answers
221 views

Creating new constructors to overwrite the existing instance variables for testing purpose

Is it good/bad practice to add more constructors just for test purposes (to mock the DOCs used in my SUT) like this : public class A { private B b = new B(); private C c = new C(); ...
0
votes
1answer
51 views

Should I postfix application.yml?

My project structure (Spring / maven): src / main / resources / application.yml src / test / resources / application.yml This is working well (when I launch test, it takes the test application.yml). ...
1
vote
0answers
25 views

Periodically update files from s3 in a Java maven library used as Packaged JAR

I have an application say x which uses another Java library(say Y) as a JAR file . So y basically needs to download a file and load it into memory before any of it's methods are called at runtime by X....
0
votes
2answers
224 views

OOP design problem with Java

I have part of the system that looks like this: Basically, the client decides which API call to invoke on the server based on the flag that is previously provided. It knows in advance which type of ...
0
votes
2answers
422 views

Is it beneficial to throw a caught IOException as an UncheckedIOException in order to prevent NullPointerException?

Not using UncheckedIOException, NullPointerException possible public void callerMethod() { Object result = ioMethod(); // call instance method of result } public Object ioMethod() { ...
-1
votes
1answer
113 views

What options are for a GUI for a Java application? [closed]

This might be a bit of a wide question. I have a couple of classes in Java that take input a String which is a mathematical expression and evaluates it. I want to implement a desktop GUI for this. ...
-1
votes
2answers
126 views

Dealing with data download which could take days to download

I have a situation on the front end where a user clicks on “Download” button and the query behind the scenes is going to take over a day. I have a Spring boot web app running as far as web services ...
-1
votes
3answers
604 views

Multiple classes working on an object

This might be a bit of a wide question but I want to understand if it's the correct way to do it. Assume I have a couple of classes all doing operations on an object. First class takes the object, ...
0
votes
3answers
153 views

Java, pattern to restrict field usage in subclasses to after calling specific method

I want to extend class Base which is defined in specific API and the extend the extended again. As you can see below, draw method of intermediate class calculates variable top and I need to use it in ...
1
vote
1answer
172 views

Can (Java) Interfaces be referred to as APIs? [duplicate]

I am trying to understand what exactly an API is. From my current understanding, an API (Application Programming Interface) is a contract provided by a software instance, that guarantees a defined ...
0
votes
1answer
51 views

Optimising network data usage in vehicle tracker

I'm currently working on an Android application that broadcasts the location of the device every 5 seconds. Pseudo showing current app process: deviceLocation = getLocation() if (hasChanged(...
30
votes
9answers
8k views

Why do we need a Builder class when implementing a Builder pattern?

I have seen many implementations of the Builder pattern (mainly in Java). All of them have an entity class (let's say a Person class), and a builder class PersonBuilder. The builder "stacks" a variety ...
1
vote
3answers
321 views

Language of an interpreter

I am trying to make my own programming language by making an interpreter for it in Java, but lately people have been telling me that an interpreter needs to be in the same language as the one it is ...
0
votes
1answer
42 views

Some Questions on PHP execution?

I am new to PHP . I have primarily worked on java. I want to run the helloworld program for PHP. I got it how to do it through blog But I have got some basic questions :- Do I need to install both ...
-1
votes
1answer
114 views

Is there a difference between older versions of the JDK and newer versions that might make learning an older version bad? [closed]

Are there significant differences between, say, JDK 7 and JDK 11 (which latter I believe is the newest version of the JDK), where significance is defined by a change which might make someone who has ...
1
vote
0answers
163 views

How to simplify design and remove duplication in java layered architecture?

I've the following project structure (simplified): domain dto view In domain model is kept. dto classes are used to pass data from domain to view. And in view I've a controller and a handful of *req,...
0
votes
2answers
66 views

Preferred method signature for API method involving DB interactions

I recently got into discussion about how the method signature should look like for an API method which doesn't expose internal objects used by application to caller. Here is how situation looks: Our ...
0
votes
1answer
125 views

Java Design Pattern to Use for switching between 2 different endpoints

I have a legacy service that is running on-prem and have built a new service that runs on cloud which provides the same functionality. I have another java (spring) service which consumes the on-prem ...
2
votes
0answers
211 views

Division by zero Java [closed]

The behavior of Java is explained in this topic. But, is Infinity useful for something? Float: public class DivisionByZeroFloat{ public static void main(String[] args) { System.out....
2
votes
2answers
397 views

Is it bad practice when an Object has to be aware of another Object

I was reading this page and came across this sentence in the accepted answer: I don't like A directly knowing about B. But that's a DIP thing not a POJO thing. What if you can't abstract out B ...
0
votes
1answer
53 views

Add the database layer to your application with a service or include it directly?

I am planning to set up a new Web-application that contains the following things: I want to use JavaEE with JSF for the Frontend & Backend combined with git-hooks to deploy them onto my server. ...
1
vote
2answers
154 views

Is passing an ErrorMessage to be populated inside the receiving method an anti-pattern?

I come from a Java background, and my colleague is from .NET. We are working on a Java project and I saw him create a method like this: public Object myMethod(Object[] param1, ErrorMessage ...
3
votes
1answer
380 views

Working with maven and multiple git repositories

We recently migrated from SVN, with most code in a single repo, to git, with most projects in their own repos (about 70 of them). We build about a dozen different apps from this java source. The apps ...
3
votes
1answer
267 views

Java - What is the “schism” between interfaces and classes that James Gosling talked about? [closed]

Recently, I've stumbled upon an old interview with Dennis Ritchie, Bjarne Stroustrup and James Gosling, published back in 2000. Each of them talked about the language they were most involved with, ...
2
votes
3answers
179 views

Wrapping Object VS Copying Fields

Almost every software has a number of internal data structures that we don't want to expose externally (through APIs for example). Let's say we have a few classes representing 1:1 database tables - ...
1
vote
1answer
149 views

Why is using an internal attribute to filter objects considered OK, but when I use a map to filter based on attributes, it's considered a code smell?

A long time ago I asked about using an enum to essentially use as a poor version of instanceof to make decisions about an object. As stated in this answer: When your weapon types enum just mirrors ...
-1
votes
1answer
125 views

Have an object communicate with another object

I asked similar question earlier this morning, and for whatever reason, I'm not getting a complete answer to my question. I'll ask it a different way. I was attempting to understand Eric Lippert's ...
2
votes
2answers
107 views

Using a Collection with Attributes to Check and Make Decisions

I was reading on this SESE page about using a variable to indicate the object type, more specifically, an enum. The accepted answer states: When your weapon types enum just mirrors the class ...
1
vote
2answers
154 views

When should we use wrapper classes for data structures / what is the ideal number of packages

I recently wrote a program in which I wrapped a map data structure to increase the readability of code like so : package wrappers; import model.primary.customer.Customer; import java.util.HashMap;...
4
votes
2answers
215 views

How should I sub class a class that constructs its objects primarily using Static methods

I have a class, that takes a lot of esoteric parameters to construct an object. I didn't write the code and frankly speaking, I don't understand completely, all of it's nuances. There is a valueOf(...
4
votes
1answer
472 views

Why do we imlpement the Command design pattern like this?

I trying to learn the Command design pattern, I already know how it works and where it is used, but I'm a little bit confused about the implementation. So I know we need to set the context by ...
0
votes
1answer
86 views

Can a user read static member values?

For my Android application, I wanted to store values such as username, userid and stuff in a sharedpreferences object. However, when I read more about it, rooted users can still have access to those ...
2
votes
0answers
193 views

Is this an example of cyclic dependency?

I'm an undergraduate student and I have this assignment on a Discrete Event simulator. Currently I have completed to the end of v1.2 here. Attached also, is an algorithmic overview. I have wrote my ...
2
votes
0answers
178 views

How to handle a large number of optional parameters

I am currently developing on a small library allowing to read and write Java .properties files while retaining all the formatting (comments, whitespace, etc.): https://github.com/hupfdule/apron This ...
1
vote
2answers
889 views

Streaming file content efficiently without risking resource leaks

I have to rectifiy an application that basically serves content by using two REST endpoints. Both endpoints transfer files to the browser, one endpoint does so by providing the file in binary format, ...