#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    How do I handle this reference problem in java?


    Code:
    class foo{
        Dog dogReference;
    
        foo(Dog _dog){
          dogReference = _dog; 
        }
    
        public bar(){
          dogReference.name = "Fido"; // fail -- doesn't alter the external _dog
        }
    }
    How do I manipulate dog data from within the foo class?
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    How do I manipulate dog data from within the foo class?
    Use the reference to the Dog class to access data in the Dog class.

    Can you post a small, complete program that compiles, executes and shows the problem?
  4. #3
  5. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,553
    Rep Power
    1621

    Manipulating data of an instance of class variable


    Accessing and manipulating data of a class member within a class using direct reference is a bad practice and it completely beats the purpose of encapsulation in object-oriented software design.

    Getter/setter methods can be declared for the purpose of data access by a 3rd party class, where all members in the Dog class should be declared private or protected. The data/class members in the Dog class that are supposed to be accessible by other classes should have their respective getter/setter methods declared in the class itself, and all other classes should only allowed to access/change the values of these data/members via these getter/setter methods.

    java Code:
     
    public class Foo{
        Dog dogReference;
     
        public Foo(Dog _dog){
          dogReference = _dog; 
        }
     
        public bar(){
          dogReference.setName("Fido"); 
        }
     
        public void makeDogBark(){
            System.out.println(dogReference.getName());
        }
     
        public static void main(String args[]){
            Foo foo = new Foo(new Dog());
            System.out.println("name of the dog before bar()");
            foo.makeDogBark();
            foo.bar();
            System.out.println("name of the dog after bar()");
            foo.makeDogBark();
        }
    }
     
    public class Dog {
     
        private String name;
     
        /**
         *  Constructor.
         **/
        public Dog(){
            name = "Dido"; //default name.
        }
     
        /**
         *  Setter for name.
         **/
        public void setName(String _name){
            name = _name;
        }
     
        /**
         *  Getter for name.
         **/
        public String getName(){
            return name;
        }
    }
    Last edited by tvc3mye; January 2nd, 2013 at 02:01 AM. Reason: missed out some words and added sample codes
    When the programming world turns decent, the real world will turn upside down.

IMN logo majestic logo threadwatch logo seochat tools logo