LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-06-2007, 04:00 AM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 122

Rep: Reputation: 15
Returning a private object in Java


In a book it says that returning a reference to a private instance variable of a class type(via an accessor method) can cause a privacy leak. Can someone show me a code example of this?
 
Old 10-12-2007, 10:21 AM   #2
cheeseplz
LQ Newbie
 
Registered: Oct 2007
Posts: 5

Rep: Reputation: 0
I could, but that would require effort ;-)

Accessor methods are designed to provide read-only access to private (or protected) variables. Remember that primitive types such as int and float are passed and returned by value, whereas reference types (objects) are passed and returned by memory address.

What this means is, when you return an object, you return its memory address and nothing can stop the calling method from modifying the data stored at that memory location; i.e. you just gave the calling method write access to that private variable.

Hope that made sense ;-)

Cheers,
Lee
 
Old 10-12-2007, 11:02 AM   #3
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Code:
public class MiscTest {
	public static void main(String args[]){
		
		Person p1 = new Person("person1");
		Height p1_h = p1.getHeight();
		p1_h.setInches(6);
		
		Height p1_h2 = p1.getHeight();
		System.out.println("Height  = " + p1_h2.getFeet() + 
							"feet And " + p1_h2.getInches() + 
							" Inches");
	}
}

class Person {
	private String name;
	private Height height;
	
	public Person(String p_name){
		name = p_name;
		height = new Height(5, 11);
	}
	
	public Height getHeight(){ return height; }
	
}

class Height {
	int feet;
	int inches;
	
	Height(int p_feet, int p_inches){
		feet = p_feet;
		inches = p_inches;
	}
	
	void setInches(int p_inches){
		inches = p_inches;
	}
	
	int getInches(){ return inches;};
	int getFeet(){ return feet;};
}
Here you go, inches should be "11" but they are now changed to "6" because:
Code:
Height p1_h = p1.getHeight();
p1_h.setInches(6);
broke the encapsulation and changed the value of a private data member.

This is so stupid!
 
  


Reply

Tags
java, member, private, vulnerability


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java: java.util.Hashmap doesn't work for a custom object nocturna_gr Programming 2 04-15-2007 04:25 PM
Java 3D object embedding in a browser ksgill Programming 1 12-20-2006 06:21 AM
compile error while returning STL iterator as object * rameshmiraje Programming 2 06-20-2004 02:50 PM
returning a reference to Object C++ chens_83 Programming 3 10-10-2003 10:16 PM
Returning an object Mohsen Programming 12 08-07-2003 03:41 AM


All times are GMT -5. The time now is 12:44 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration