• Post Reply Bookmark Topic Watch Topic
  • New Topic

How is a class different from a struct?  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
anyone can help to answer this? thanks a lot !! T^T
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jojo chin wrote:How is a class different from a struct?


Well, since Java, unlike C/C++ (which I am assuming is the basis for this question), does not have a direct concept of struct (or union), then arguably, discussing the difference does not make sense. Or perhaps, you are asking whether you can use a class declaration like a C/C++ struct declaration?

Henry
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:Well, since Java, unlike C/C++...

And C# (one of its better decisions, IMO), where I believe they are somewhat interchangeable.

However, Henry is right: there's no such thing as a struct in Java.

Winston
 
Marshal
Posts: 56605
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't think of classes, but of objects. Look on a class simply as instructions to make an object.
A struct only has data (fields) but an object incorporates data (fields) and behaviour (methods). If you want to do anything with the data in a struct, you have to write a procedure elsewhere. you should deal with the data in an object inside the same object.
It would be quite easy to write a class to create objects which only contain data.
 
jojo chin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry, Winston and Campbell. You all help me a lot!! now I understand than before ^_^
 
Saloon Keeper
Posts: 7993
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, that wholly depends on your definition of 'struct'. As Henry rightly points out, Java doesn't have structs, so the comparison is meaningless unless the OP explains what they mean by struct.

There's a large difference between structs in C++ and C#. Your description is similar to the C++ definition. C# structs however, can have methods. They are different from C# objects in that objects can only be manipulated through references, while structs can't have references to them at all.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of value type semantics in C#:
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I meant structs as in C/C++; I haven't seen that sort of C# struct, which does blur the distinction from a class, as Winston said.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!