• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Complex data structures in Java

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic