• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Are there structs in Java? Are they even called structs?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Can someone please tell me if there are structs in Java? I am familiar with C programming, thats why I ask, but when I search for 'Java structs' I havent really found anything and I suspect that just structs are simply different and may be called something different so I thought I'd ask.

If there are structs can someone please write like a 2-3 line code declaring a struct with strings and integers, I would just like to see how it works, how the declarations look, and how they are used in 'main'.


Thank you.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you just use classes with public instance variables?


On the other hand, due to Java's strict typing, unions are harder to implement -- you can sortof have it with bytebuffers, but it's not the same.

Henry
 
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no structs in Java. Classes like Henry mentioned are the closest thing, but you still need the "new" keyword to create them.
 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I wrote some sample code trying to implement structs using classes but it is not really working out. The program compiles, and then throws an exception (NullPointerException).

Can anyone put me on the right track? I don't understand why a null pointer exception is thrown to begin with, the program is coded in a way that I just store information in the slot zero of the array of classes, then I try to print it out. My logic suggests that this should work, but I am sure my logic FAILS once again lol.

Honestly, any help or fix to this simple program will be really really appreciated.



Thanks in advance.
 
Rob Spoor
Sheriff
Posts: 22702
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Hazan wrote:


That creates an array that can store 50 List objects. Each element is still null though. You must initialize them before you can use them:
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob is right, but did you really want to call your class "List"? That's the name on an interface in the java.util package you use to create lists. Call it Student and your code will be less confusing.
 
Greenhorn
Posts: 5
Spring AngularJS Flex
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a interesting read about Structs usage in Java http://www.javapractices.com/topic/TopicAction.do?Id=256
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something to avoid, more likely. Because the fields are not private you are losing encapsulation and those are equivalent to global variables. You are losing all the advantages of object orientation. If you want to use structs like that, go back to C, which lacks the concept of public and private.
 
Saloon Keeper
Posts: 14515
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not possible to create something that looks like structs in Java. With a struct, you're interacting with the data type directly. In Java, you always handle objects through references.
 
Stephan van Hulst
Saloon Keeper
Posts: 14515
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing in the article is not a struct. With a struct, it would be possible to do the following:

This code doesn't work in Java. Line 8 would print "The Two Towers", because Book is a reference type and not a struct.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic