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.

learn more… | top users | synonyms

5
votes
6answers
332 views

Breaking the contract: throw exception or do nothing?

Let's say we have a list of players in our Event class. And we have a dictionary with the score of each player. We can add a score to a player using the addScore method: public class Event { ...
0
votes
3answers
138 views

Should I check for nulls in setters and similar methods?

I have plenty of setters in my classes, as well as many methods to add an item or a set of items to lists or maps. Should I check for nulls for each one of them, or should I allow ...
1
vote
1answer
151 views

Improving a try/catch

I'm a python programmer trying to get to grips with Java's inflexibility; I'm trying to parse a date from a string into a Calendar object private Calendar parsedDate ( String dateString ) throws ...
4
votes
2answers
156 views

How to design an iterable but immutable (read-only) collection

My program models a sport tournament which has one or many events or categories. The class Event has members like players or courts as a List, as well as some dictionaries using the class Map. So far ...
1
vote
1answer
193 views

Logging RestTemplate's responses

I want to log responses using Spring's RestTemplate. At first I thought I should do it using a ClientHttpRequestInterceptor, but since every set of requests should be logged to a different file, I ...
0
votes
1answer
96 views

How to make interfaces usable for special classes only?

Is there a way to make Java interfaces only implementable by classes of a special type? So for instance, I have a class Foo and an interface Bar. Only subclasses of Foo should be able to implement ...
-1
votes
1answer
112 views

Can I install Apache HTTP server and Apache Tomcat server on the same host?

I want to practice web programming on my Ubuntu virtual machine. I want to learn both server side web development using Java/J2EE and client side coding (HTML/CSS/JavaScript) . I heard that we need ...
0
votes
1answer
66 views

Is it a good practice to use a Service without Delegate?

I have a simple REST application with a single controller. The application needs to fetch data from a Database, convert it to JSON and return this to the caller. I need to decide what layers I should ...
2
votes
2answers
85 views

Java EE exceptions for validation and APMs

In my current work, we have some Java EE applications and we use exceptions and exception mappers to deal with user errors, i.e. that can't be dealt with in the frontend. We usually reply with a 400, ...
1
vote
2answers
254 views

Is saying “there are no threads in nodejs” correct?

Perhaps I am getting something wrong here but I had a conversation today that left me perplex. I hear a lot about nodejs not having threads. but that's not entirely true, right? The precise ...
6
votes
2answers
241 views

Data validation: separated class or not?

When I have a lot of data that needs to be validated, should I create a new class for the sole purpose of validation or should I stick with in-method validation? My particular example contemplates a ...
3
votes
5answers
199 views

Method returning an unmodifiable list

In Java you could return a list that cannot be modified by the caller by doing return Collections.unmodifiableList(list). Should the method name indicate that it's returning an unmodifiable list? ...
1
vote
2answers
226 views

What is the best data structure to store 2 keys and 3 values?

Well, I am having two keys and 3 values for that. Say key1,key2,value1,value2,value3. In future may the values can be increased like value4, value5 so on. I need to get the ...
7
votes
5answers
189 views

Open Closed principle in design patterns

I am bit confused about how Open Closed principle can be applied in real life. Requirement in any business changes over the time. According to Open-Closed principle you should extend the class instead ...
2
votes
2answers
78 views

Is it possible to capture java objects to save them as test resources?

I'm wanting to write unit tests to check that for given database responses, certain objects are create/things are done. The problem I have, is that when mocking the database response, I have to ...
1
vote
1answer
78 views

Most efficient way to keep count of objects in State X without constantly counting

The application I work on manages hundreds of thousands of records each in a different state in their lifecycle. A large requirement is to have a nearly accurate count of these objects in each state ...
4
votes
2answers
134 views

Understanding polymorphism and interface in Java

I'm reading though some notes. And I'm not understanding the following two statements. Polymorphism means that it is always the class of the actual object at run time that determines which method ...
24
votes
7answers
3k views

How to return warnings alongside result of calculation to caller of a Java method? [duplicate]

I have a class, lets call it Calculator, with a method like this: public double[] performCalculation(double[] someInData) This method can generate a number of non fatal warnings (represented as an ...
-1
votes
1answer
88 views

Should I re-write VB6 Code to Java [closed]

I work at a mid-sized company where an update is needed to an piece of the application due to an addition to the environment. The part that needs updated has been in place for 13 years, is fairly ...
1
vote
2answers
70 views

One application's methods invoked by many other applications

I have a small Java console application that runs a set of data through an algorithm. Given the size of the data and compute time, I often run 10 to 15 instances of the application, each working on a ...
27
votes
5answers
5k views

Is overriding concrete methods a code smell?

Is it true that overriding concrete methods is a code smell? Because I think if you need to override concrete methods: public class A{ public void a(){ } } public class B extends A{ ...
-2
votes
1answer
156 views

What good Java template engine should have?

Im starting to write my own template engine for Java. It will be primarily intended to Spring Framework. I usually use Freemarker, but I know template engines such as Java Server Pages, or Thymeleaf. ...
2
votes
1answer
105 views

Deciding the best way of modeling a timeslot or period of time for a schedule

I am making a sport events scheduler to calculate possible schedules out of initial data and configurations. There are three main domains: players, courts and timeslots. My question regards the ...
2
votes
2answers
124 views

Why would an interface override methods of the interface(s) it extends in Java 7?

I was looking at Map and SortedMap documentation from Java 7 and I have realized that SortedMap, which extends Map overrides entrySet(), keySet() and values(). AFAIK, interfaces cannot implement a ...
4
votes
4answers
296 views

Should an object be able to represent itself across components?

I'm working on a networked game. I have tried to design the various components (client, server, engine) to only about things within their domain. For example, the server should only be concerned with ...
1
vote
1answer
38 views

Eventing solutions for java legacy applications too old for real JMS queue/topic eventing?

What are some architectural solutions that can mimic eventing for systems (Java) that are too old to implement eventing solutions, such as conventional Queue/Topic based JMS messaging? I have a ...
0
votes
1answer
85 views

Design Pattern for Object Updates

I have a "Product" class that has some ID, and multiple other fields, and a function updateProduct(id, Product update). The intention is that "update" only contains the changed information - other ...
-3
votes
1answer
124 views

Using System.out.println() with loops and arrays [closed]

Is there anyway I can create a loop for System.out.println("...") and reading in the values? For example I wish to do this: System.out.println("Input the Executive's name: "); String eName = ...
0
votes
0answers
43 views

How to save the differences between objects which are the result of random changes?

I keep a state object of the following form: class State { int num1, num2; List<String> strings; MyObject myObject; // A class I wrote // etc. } The state changes many times ...
4
votes
2answers
140 views

Question about Java's proposed value types

I'm reading the Project Valhalla proposal to add value types to Java. It's arguing for explicit value types because the compiler can't automatically move allocations from the heap to the stack very ...
2
votes
4answers
187 views

Should I initialize a List when declaring it or should I use a static block?

Could anyone explain which one of the two snippets below would be best practice for initializing a static final List? Initialize when declaring: public class Sample { private static final ...
0
votes
0answers
14 views

Visibility into Tomcat & WebApplication ClassPath loads

I am not sure if this is a thing that can be done, or if it is way to dependent on my development environment: Is there a quick way to see which applications are loading in which jar files for class ...
-1
votes
2answers
188 views

Why is referencing arrays, instead of copying them, bad? [closed]

Is it because the advantage of copying a array into another array is that you can have the data added onto the array itself, rather than just referencing a particular memory location?
2
votes
3answers
109 views

Dealing with runtime exceptions when reading from file

Let's say I read a bunch of numbers from a text file. Each line is initially a string, I need to parse it to an integer. This is where the first exception might happen - NumberFormatException thrown ...
5
votes
1answer
145 views

Three classes and get methods

class A{ private List<T> list; public int getListSize(){ return list.size(); } } class B{ private A objectA; public int getSizeOfListInA{ return ...
2
votes
1answer
129 views

Supporting Multiple Java Versions in OSS Libraries

I am in the process of standing up my first OSS Java lib (GitHub/Maven) that an open source hardware community will be making fair/moderate use of. I am writing this library with Java 8 and managing ...
2
votes
4answers
512 views

Why isn't there a primitive “complex number” type in Java?

Does anyone know why something like http://www.ipd.uka.de/JavaParty/cj/#Down was never completed and integrated into mainline java? This seems like a no-brainer... I realize the java gawds dont want ...
5
votes
1answer
65 views

Storing pairs in a map

If I have a Pair class like this: class Pair<K, L>{ public final K a; public final L b; public Pair(K obj1, L obj2){this.a = obj1; this.b = obj2;} //... } I want to associate a ...
1
vote
1answer
61 views

How to detect network interruptions

I want to write a kind of "diagnostic function" which is able to detect when a TCP/IP based network is interrupted. I would like to connect two endpoints and detect when an established connection ...
2
votes
3answers
129 views

What is the least bad way of knowing the type of a key from Json file

in our app we have a Json file that contains several properties used for menu configuration. One of the keys should tell us if a menu item should be displayed. The thing is that the value for this ...
0
votes
2answers
92 views

Accessing C methods from class A

class A { B bObject; } class B { private List<SomeType> list; public List getList() {return list;} public void foo(int i) { list.get(i).someTypeMethod(); // 2 dots } ...
2
votes
1answer
81 views

Constructor Injection vs Field Injection

I know this topic has been discussed a lot already so I don't want to get into which one is the "best way". I have been using field injection for a couple of years now but recently I discovered that ...
-1
votes
2answers
83 views

Which is better either creating two objects for two threads or one object for two threads?

In the below program, I'm calling method1() of SampleClass from two threads. So, basically I have created two objects for each thread and called the method1(). I have also seen the scenario of ...
-2
votes
3answers
180 views

Is it possible to have private or protected objects in Java? Or are all objects considered public?

Is it possible to have private objects? For example, when you instantiate an object (classType object1 = new classType()), can that be private or protected or is it always public?
0
votes
1answer
69 views

How do you write super() in the UML format?Is it correct to write super(parameter: type): return type in the UML methods section?

Suppose you write a subclass that extends to a certain class and in that subclass, you use the super() method for your constructor. Would you write "super(parameter: type): return type" in the UML?
-1
votes
1answer
59 views

Enable matching of character sequence made up of regex control character

Java Pattern provides flags that enables you match regex control characters. Passing Pattern.LITERAL or 16 as second argument will match any Unicode char, but not if you use a word bound with the ...
2
votes
2answers
170 views

What is a good approach to get a detailed system design from an existing system?

I will be working on a system which will be ported over from Java to .Net. It is a pretty big system. I don't have any specifics about how the system is designed, no documentation, nothing. You know ...
2
votes
2answers
176 views

Mutable objects - setters and getters

Is it a good practice to have a setter method of this kind? With primitive types, it's obviously fine, but when you have a setter for a field which holds a reference to mutable object, this might go ...
3
votes
2answers
164 views

How do people make libraries cross-platform?

How do people make libraries cross-platform (UNIX-systems and Android, sometimes even iOS)? What do they use? I'm asking this because I make libraries as a hobby and I'm curious.
6
votes
3answers
364 views

Best practices for using public, protected, private?

Is it fair to say that it is good practice to default everything to private up front when coding something? And then only upgrade it to protected if a subclass needs it, or public if another class ...