Skip to Main Content

Java SE (Java Platform, Standard Edition)


For appeals, questions and feedback, please email

Session object in my own set of classes

6af000ca-9cf8-4256-9dd3-624348de7631Apr 14 2020 — edited Apr 14 2020

Good afternoon

I work with the API for the desktop application.

I wrote for myself a small, so to speak, class library at a higher level of abstraction.

In many of my classes, access to the session of this application is required (you need to create a Session object and assign the current session from the application to it, and already work with these objects).

Tell me, please, how should I better organize access to this object?

I used to create a new session object in each class in the specified way, but then I decided that this code repeats, and that this is not very good, and began to pass this object to methods as a parameter. But now I don’t like it either, because this object is passed to many methods and this is also a repetition, and generally not very good when there are a lot of arguments in the methods.

I thought what a parent class could do, which will have this object as a static field, and at the very beginning of my program, assign the current session to this field of this class, and other classes will already have access to it without any additional code. You can make just a separate singleton class with a session. Which option is definitely better, I do not know.

Thanks in advance

Yours faithfully!

Post Details
Added on Apr 14 2020