SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: class reference

  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    class reference

    I am working on an assignment but I got stuck in a step, I basically have 2 classes and 1 interface, class1 contains 2 constructors and 2 methods, class2 implements the interface everything is working fine however I was told to create a reference to class1 in class2, I have been searching around but I am having trouble finding information, can someone point me to the right direction of a document where I can find information on what a reference to a class is and how to add it, I have found reference to objects but not to classes
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    All Classes are Objects.

    A reference to a Class is created when one instantiates a Class.

    In Class2:

    Class1 referenceToClass1 = new Class1();

  3. #3
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the clarification!
    Do you get bothered because I do the same thing every day?
    Do you question why I do it?
    Then find something that you actually like doing!!!

    Stop thinking on what I do.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2007
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

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