aspose file tools*
The moose likes Beginning Java and the fly likes Complex data structures in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Complex data structures in Java" Watch "Complex data structures in Java" New topic
Author

Complex data structures in Java

Tony Bateman
Ranch Hand

Joined: Mar 21, 2005
Posts: 38
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Mar 21, 2005
Posts: 38
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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

Joined: Mar 21, 2005
Posts: 38
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

Joined: Oct 13, 2005
Posts: 38340
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Complex data structures in Java