Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Complex data structures in Java

 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I come from the SAP ABAP world where it is easy to construct simple but deep nested structures of data. What is the best way to do the equivalent in Java? Is there a way to define pure data structures without any methods? Or am I going to be told that is bad practice because the data should always be verified by getter and setter methods?

ABAP is probably not a good language to learn programming with; it is an old language that has been constantly upgraded but lends itself to potentially poor practices.

Any help appreciated.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you use data structures without methods?
It is worth drawing a diagram of what data structures you would require, how they work, and them compare that with the ready‑made structures. Also look for something like Apache Commons, which may have some more specialised collection classes.
 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Structures and data are declared like this:

TYPES: BEGIN OF struct,
number_1 TYPE i,
number_2 TYPE p DECIMALS 2,
END OF struct.

DATA: wa_struct TYPE struct,
number LIKE wa_struct-number_2,
date LIKE sy-datum,
time TYPE t,
text TYPE string,
company TYPE s_carr_id.

The data is accessed in a program by referring to it directly. For example if I wanted the value stored in the date field, I would reference it by writing wa_struct-date. The data structures are comparable to classes without any methods I guess.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can define a pure data holder without any methods, and yes, it's bad practice. Java is an OO language, and a couple of core concepts in OO that go against that are 1) Objects have behavior and 2) Hide the details of your implementation from the outside world.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like something in C.
Yes, you can have classes with no methods, but that sounds like poor non‑object‑oriented design. A class should be responsible for itself, so it should supply all the behaviour it requires by itself.
 
Tony Bateman
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, to summarise:

1) Use a class as a data storage element,
2) Use setters and getters to ensure he data is consistent and hidden from the outside world.

Is that correct? is there anything else that could be relevant best practice?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. If there is any manipulation of those data, consider doing it in the class which encapsulates them. A Circle class might have centreX, centreY and radius fields. You would calculate its area with πr² in a getArea() method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic