Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 1st, 2013, 11:52 AM
AngleWyrm AngleWyrm is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 AngleWyrm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 24 sec
Reputation 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?

Reply With Quote
  #2  
Old January 1st, 2013, 05:54 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: Eastern Florida
Posts: 2,951 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 29 m 42 sec
Reputation Power: 345
Quote:
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?

Reply With Quote
  #3  
Old January 2nd, 2013, 01:42 AM
tvc3mye's Avatar
tvc3mye tvc3mye is offline
Daniel Schildsky
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2004
Location: KL, Malaysia.
Posts: 1,534 tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level)tvc3mye User rank is General 10th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 2 h 27 m 57 sec
Reputation Power: 1620
Send a message via MSN to tvc3mye Send a message via Yahoo to tvc3mye Send a message via Google Talk to tvc3mye Send a message via Skype to tvc3mye
Facebook
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:
Original - java Code
  1.  
  2. public class Foo{
  3.     Dog dogReference;
  4.  
  5.     public Foo(Dog _dog){
  6.       dogReference = _dog;
  7.     }
  8.  
  9.     public bar(){
  10.       dogReference.setName("Fido");
  11.     }
  12.  
  13.     public void makeDogBark(){
  14.         System.out.println(dogReference.getName());
  15.     }
  16.  
  17.     public static void main(String args[]){
  18.         Foo foo = new Foo(new Dog());
  19.         System.out.println("name of the dog before bar()");
  20.         foo.makeDogBark();
  21.         foo.bar();
  22.         System.out.println("name of the dog after bar()");
  23.         foo.makeDogBark();
  24.     }
  25. }
  26.  
  27. public class Dog {
  28.  
  29.     private String name;
  30.  
  31.     /**
  32.      *  Constructor.
  33.      **/
  34.     public Dog(){
  35.         name = "Dido"; //default name.
  36.     }
  37.    
  38.     /**
  39.      *  Setter for name.
  40.      **/
  41.     public void setName(String _name){
  42.         name = _name;
  43.     }
  44.  
  45.     /**
  46.      *  Getter for name.
  47.      **/
  48.     public String getName(){
  49.         return name;
  50.     }
  51. }
  52.  
__________________
When the programming world turns decent, the real world will turn upside down.

Last edited by tvc3mye : January 2nd, 2013 at 02:01 AM. Reason: missed out some words and added sample codes

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > How do I handle this reference problem in java?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap