Results 1 to 6 of 6
  1. #1
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default struct's in Java?

    I will give an example below of what I am talking about. in C++ I know I would need a copy constructor because I would be pointing blindly in the heap if I deleted a object in the struct.

    We have a Class/struct animal and three animals in that class/struct, a dog, a cat and a cow. Each animal has a baby and then I will give the animal a name in main. But if I delete one of the dogs puppies because I sold him wouldn't I being pointing off in the heap and have a leak?

    Also since Java doesn't have structs can I put a class inside a class?
    Last edited by jocdrew21; 03-17-2014 at 01:33 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: struct's in Java?

    Forget about memory management in Java. There's nothing you have to worry about.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: struct's in Java?

    Quote Originally Posted by PhHein View Post
    Forget about memory management in Java. There's nothing you have to worry about.
    On regular PCs anyway, if you do embedded Java then its a little more interesting.

    "A class inside a class" is called an inner class and yes, you can do that. But why do you ask? What exactly about a struct are you missing in Java?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

    Default Re: struct's in Java?

    But how would you give class animal a object like dog and the dog a object like puppy? You still have to create a puppy for the dog every time you give it a new name right like:

    class animal()

    class dog
    {
    string puppy;
    }

    What is not clicking here with me.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,895
    Rep Power
    5

    Default Re: struct's in Java?

    if you would spend a little more time studying the language features in stead of jumping ahead trying to get by with your knowledge of C++, you'd have more chance of things clicking :s

    in any case I don't understand your last question. Of course you need to create a new INSTANCE of a dog for each puppy; that's how it works in the real world too. Each puppy is a different living entity with its own name.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    62
    Rep Power
    0

Similar Threads

  1. Java Struct
    By akl in forum New To Java
    Replies: 4
    Last Post: 09-20-2011, 06:51 PM
  2. struct problem
    By amitabh in forum Web Frameworks
    Replies: 1
    Last Post: 09-01-2010, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •