• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Access Properties of Object Within an Object

 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working in new territory for me.

I have a class with several properties.

In another class I create an instance of the previous class, using parameters from this parent class, and assign it to an object property of this class.

Note: snObject is the parent object.


No how do I access the properties of the embedded class?

Maybe this is not the proper way to do it.

Normally I would create properties in the parent class that would hold the values of the child class. But I know there has to be a cleaner way.
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by parent object? It sounds as if you had an incorrectly‑written constructor there. If you are passing a “parent” object to a constructor, you may be allowing a this reference to escape.
Single responsibility principle: The class should take care of itself. If you have a serial number which may be added to the object later, give it a method
 
Stephan van Hulst
Bartender
Posts: 6584
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steve, welcome to CodeRanch!

Your question is a bit unclear, because you're using a few words that may have a slightly different meaning than how you're using them. I'm assuming you mean the following:

Using parameters from this parent class: using fields of the constructor argument.

Object property: instance field.

Embedded class: constructor argument.

In particular, I don't understand what you mean by the parent/child relationship between the two types. What exactly is the problem with accessing the properties of the object that you passed to the constructor?

Maybe you can also give us more context about the program you're writing.
 
Steve Dyke
Ranch Hand
Posts: 1718
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Maybe you can also give us more context about the program you're writing.


Maybe I can start from top down. I am sure I am not doing this right but that is the reason for post so I can learn.

In my web app I have jsp files using EL to display object field values.

The object is scoped to a session attribute in my servlet which displays the jsp.

The servlet creates an instance of the serialnumber object, I am referring to this as a parent object or an object that contains objects.

The object has fields and is used to create an instance of another object named noshipproperties. I am referring to this as a child.

This child object has fields.

Now back to the serialnumber object. When the noshipproperties object is created I assign it to an object field of serialnumber. Note: As I said I would normally create fields in the serialnumber object to assign field values from the noshipproperties object.

So my question is in the jsp EL how do I reference the field values of the noshipproperties object?

I hope I have explained it better. And I very much appreciate all the help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In EL you don't access fields, you access properties. Remember, the EL is bean focused. So you need to make sure that each property you want to access has a proper accessor (getter).
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:I have a class with several properties.

Let's stop right there. Like the others, I don't know what you mean by "properties", but I'm going to assume that you mean 'members' or 'fields' - ie, things defined IN the object.

In another class I create an instance of the previous class, using parameters from this parent class...

And again I'm going to stop you. In Java, the word "parent" usually refers to a superclass - ie, one that is extended by another. Eg:
  public class Dog extends Animal { ...

but what I (and the others here) suspect is that your original class contains the second one, ie:
  public class Pound { // A dog pound
    private Dog fido; ...

in which case it's a "container" or "wrapper", and 'fido' is a field.

Hope that clears up our doubts, and if I'm wrong, let us know.

If you want other classes to be able to "see" a field, then you usually create a public "getter" method for it, eg:
  public Dog getDog() { return fido; }

HIH

Winston

Edit: Just saw the latest posts, but I'll leave this up in case it helps. (Moderators: feel free to remove)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!