Here is an example of what you are asking in regards to One class referencing another.
Code:
package com.sitepoint.forums.users;
public class Person {
private String FirstName;
private String LastName;
}
public class User {
private Person person = new Person();
private String username;
private String password;
public User(Person person){
this.person = person;
}
}
If you were to get this person from a Session Variable User class might look something like
Code:
public class User {
private Person person = new Person();
private String username;
private String password;
public User(HttpServletRequest request) {
HttpSession thisPerson = request.getSession(false);
this.person = (Person) thisPerson.getAttribute("userInfo");
}
In the first example you are passing a Person object into the User's constructor. You could directly assign it to the object inside or you 'could' call the setter method inside the User constructor for the private Person object.
If you were doing a JSP based website you could grab a Person object out of the Session (This would probably be handled inside a controller though so just look at it for reference since you probable don't want to tie your object to having a Servlet jar dependency being provided in your project.)
Anyways, you'd get the session then grab the item out of the session and cast it into the type of Object you want. HashMap's work in a similar way if your map is a generic Object and you pass in a specific Object type.
Bookmarks