File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Does Java support 'structure' like C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does Java support Watch "Does Java support New topic
Author

Does Java support 'structure' like C++

Suresh Kanagalingam
Ranch Hand

Joined: Aug 17, 2001
Posts: 82
Does Java has 'struct' or 'type def' similar to C++?
I scanned some Java books and cound not find any leads!
Thanks
Suresh
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The closest java equivalent would be a class with public fields and no methods. You don't see this very often since it's considered not good object-oriented programming. Better to make the fields private and create accessor methods. But sometimes you'll see it for a private nested class used for convenience inside another class. Check out the source code for ListIterator.Entry for an example.


"I'm not back." - Bill Harding, Twister
Suresh Kanagalingam
Ranch Hand

Joined: Aug 17, 2001
Posts: 82
Thanks Jim,
But where would I find the code for 'ListIterator.Entry'?
Suresh
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Java does not support typedef. Thank God.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Your JDK should have a file src.jar or src.zip in the top directory. Unzip it (WinZip or the jar command can do this) and find the code for java.util.LinkedList. Entry is defined a static member class in that file. If too much of that is unfamiliar, then the short answer is no, Java doesn't have those features, and we do without them most of the time.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Jim Yingst wrote: Better to make the fields private and create accessor methods
Even better to think about the actual problem you are trying to solve a bit more, and write methods which actually do something useful. Wanting to use classes which are just "structures" in an Object-Oriented language is often a sign that the design of the system is not well enough understood.
As yourself what are the responsibilities and behaviour of this class? If you can't think of any, you may be better of not writing a new class at all, but just using one of the provided Collection classes or an array until you have more of a feel for the needs of the system.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Java support 'structure' like C++