Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
5answers
8k views

Is it a good practice to wrap collection in Java? [closed]

I came across a snippet like this, and find it's over engineering. Is it a good practice? public class SchoolList extends ArrayList<School> { } public class School extends ArrayList<...
-3
votes
1answer
56 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 ...
25
votes
3answers
46k views

When to use a Singleton and when to use a static class [duplicate]

I've searched about this here and on StackOverflow and found some differences between the two. But I'm still not sure in what cases one would prefer a Singleton, and in what cases one would choose to ...
37
votes
3answers
45k views

Why prefer non-static inner classes over static ones?

This question is about whether to make a nested class in Java to be a static nested class or an inner nested class. I searched around here and on Stack Overflow, but couldn't really find any questions ...
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 ...
0
votes
2answers
114 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(...
0
votes
4answers
1k views

Why is Java not 'pure' OOP? [duplicate]

Java is designed in a very OO approach, and somewhat even 'forces' programmers to program within the OO paradigm (which can be considered good or bad, a matter of opinion). However while almost ...
1
vote
1answer
77 views

Functionality design: Multiple sources, multiple consumers with configuration

It is a Design Problem which I am listing out here. I have multiple event sources in my app, each one produces events with the different set of data (but each source is producing only one type of ...
5
votes
2answers
179 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'...
-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. ...
0
votes
1answer
445 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 ...
2
votes
2answers
176 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 ...
40
votes
4answers
42k views

Why do C# developers newline opening brackets? [closed]

I've spent most of the last several years working mainly with C# and SQL. Every programmer I've worked with over that time was in the habit of placing the opening brace of a function or control flow ...
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)...
0
votes
1answer
88 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?

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 ...
2
votes
2answers
37k views

Print all values in a value object [closed]

I have to debug an issue which requires me to print all the values of a Value Object that is returned by a web service call. The Value object is a complex object in the sense, it has another object as ...
0
votes
2answers
83 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 ...
0
votes
1answer
32 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
1answer
63 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 ...
4
votes
1answer
1k views

Is there a reason Iterator and Stream do not implement Iterable?

The other day I was playing around with an experiment and I had a for loop something like so: for (Node node : children) { // do stuff with node ... } And then I changed it to do this: for (Node ...
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,...
1
vote
1answer
103 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-...
0
votes
2answers
9k views

How do you deal with NULL values while creating JsonObject?

I am learning Java and JSR-353. My question is quite forward. Having the method below: public JsonObject createJsonObject() { return Json.createObjectBuilder() .add("address", this....
4
votes
1answer
160 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
104 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
90 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 ...
-8
votes
1answer
4k views

Convert program in Java to Assembler? [closed]

I have to do a program to school in Assembler but we did just a few basic things. Is it possible to write code in Java and convert it to Assembler?
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 ...
0
votes
6answers
2k views

How to change the state of a singleton in runtime

Consider I am going to write a simple file based logger AppLogger to be used in my apps, ideally it should be a singleton so I can call it via public class AppLogger { public static String file = ...
1
vote
2answers
196 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. ...
1
vote
3answers
267 views

Is it sometimes okay to intentionally violate the Dependency Inversion Principle?

I am doing some work to refactor a class. It currently a 'God class' and contains all different logic/operations solely in that class. One of my solutions is to extract all different parts of logic to ...
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<>(); ...
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
287 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 ...
1
vote
4answers
213 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
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 ...
4
votes
5answers
18k views

Java SE and Java EE? [duplicate]

I was now talking with some people and they told me that if i was intrested in a job in the future for Java programming i should not spend too much time only learning Java SE i should spend time ...
-3
votes
3answers
160 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() { ...
0
votes
4answers
296 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
1answer
108 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, ...
1
vote
3answers
335 views

Why is the whole Hadoop ecosystem written in Java?

Developing Big Data processing pipelines and storage, you probably come across software which is more or less a part of the Hadoop ecosystem. Be it Hadoop itself, Spark/Flink, HBase, Kafka, Accumulo, ...
1
vote
1answer
107 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 ...
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
2answers
71 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 ...
5
votes
3answers
378 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 ...
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
50 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 ...
7
votes
5answers
77k views

What is the exact meaning of Platform independence?

What is the meaning of platform independence? I am taking the case of Java. Can I run a Java application on Linux that built on Windows platform? Or the reverse? Can I use a same (or exact) ...
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--...