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

3
votes
5answers
217 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
248 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 values(value1,value2,...
7
votes
5answers
213 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
90 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
87 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
138 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
93 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
82 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
207 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
124 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 latter....
2
votes
2answers
129 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
298 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
50 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
93 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
125 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
143 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
189 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 List&...
0
votes
0answers
15 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
189 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
112 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
153 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 objectA....
2
votes
1answer
133 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
535 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
67 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
63 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
134 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 key,...
0
votes
2answers
94 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
82 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
87 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
70 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
61 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
172 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
192 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
167 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
387 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 ...
1
vote
1answer
77 views

Better way to organize query methods in Android?

In my Android app I have a SQLiteHelper class that extends SQLIteOpenHelper, and takes care of things like table-creation and upgrades. SQLiteDatasource class that performs CRUD operations on the ...
3
votes
1answer
87 views

Bounded Type Parameters

Ref: https://docs.oracle.com/javase/tutorial/java/generics/bounded.html, We have the method: public <U extends Number> void inspect(U u) Why can't this be: public void inspect(Number n) ? It ...
-3
votes
2answers
58 views

class name for linked lists

I have a question about how to name a class in java. Could a class name be arbitrary or have to obey some rules? For example, I want to define linked lists in a class, does the class name have to be "...
2
votes
3answers
92 views

Calling a constructor from a parent class in a derived class

I'm trying to create a parent class with a constructor that takes a single int as a parameter. I also need to derive a child class that creates two instances of the parent class using a constructor ...
3
votes
2answers
215 views

Using MVC style, where is the best place to put SQL functionality?

I am wondering about best practices here. MVC (Model - View - Controller) patterns involve separating components of your program that model the data, manipulate those models, and display those ...
0
votes
1answer
73 views

How to import driver details and Connection details without writing it manually or hard coding?

class ConnectionUtil{ public static void main(String args[]){ try{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:...
5
votes
1answer
95 views

How can I plugin new JARs implementing an interface dynamically?

Quick background: I am working on developing an interface that will be implemented by myself and other developers. This interface will allow users to "plugin" new functional code into a system for ...
2
votes
3answers
157 views

designing classes for testability

I am writing a library for OVH api calls, and I am wondering how to make it friendly for unit tests. I have a class APIClient. The constructor of this class initializes the object with all parameters, ...
5
votes
4answers
299 views

Why instantiate an object to a Base class rather than a specific Sub class?

For example: URL blogFeedUrl = new URL("http://manishmaharzan.com.np/getJSON/json.json"); HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection(); connection.connect(); ...
-1
votes
1answer
82 views

Best approach to scheduled events in Node.JS

I'm currently writing a browser based game where events occur in real time - as an example of this, take Farmville, where a crop only grows after a particular length of time. Running a method every ...
4
votes
0answers
104 views

Is the observer pattern suitable when the observers are not independent of each other?

I have a class Car which has 2 properties: int price and boolean inStock. It also holds a List of abstract class State (empty class). There are 2 states which can be applied on the car and each is ...