Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 21090
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Abhijeet Bhanjadeo
Greenhorn
Posts: 5
AngularJS Flex Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a interesting read about Structs usage in Java http://www.javapractices.com/topic/TopicAction.do?Id=256
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • 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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • 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: 7806
142
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!