Results 1 to 3 of 3
  1. #1
    besil is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Unhappy can change private member of class from other class

    As i understood the purpose of private member of class is not to let
    uncontrol manipulation of it. i.e only the class methods can read/write
    directly this private.
    Attached below a program WHICH let me write to private member without any problem !!!
    I understand that i got from getter method a reference to this private array member ("Table") and when I have it I can access directly to this resource.
    but still as a concept I can change private member and not through the class
    methods.
    Isnt it a security problem ??????
    In addition I will be happy to hear elegant suggestion to overcome this problem.
    Thanks in advance

    public class ClassContainMain
    {
    public static void main(String[] args)
    {
    int CopyArray[];

    SecondClass Second = new SecondClass();

    CopyArray= Second.getTable();
    Second.PrintTable();

    // changes the values of the private array "Table"
    CopyArray[0]=CopyArray[1]=15;
    Second.PrintTable();
    }
    }

    public class SecondClass
    {
    private int Table[]={1,2,3,4,5,6,7};

    public int[] getTable(){
    return Table;
    }

    public void setTable(int[] table){
    Table = table;
    }

    public void PrintTable()
    {
    for (int a:Table)
    {
    System.out.print(a);
    }
    System.out.println();
    }
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by besil View Post
    Isnt it a security problem ??????

    no. private means class access and no visibility outside the class containing these private members. but what you are doing is returning a reference to your array. this member will be no longer private, because operations on the object will also alter the object. so if you want to keep it private, it's a nonsense to return its reference. to overcome your problem, you could return a string containing all elements.
    Last edited by j2me64; 02-16-2011 at 11:49 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by besil View Post
    Java Code:
    	public int[] getTable(){
    		return Table;
    	}
    
    	public void setTable(int[] table){
    		Table = table;
    	}
    These methods are to blame; although the Table array is private those two methods give it all away and allow the entire world to change the content of the array or change the array itself. If you want this better make the Table member variable public. If you don't want this behaviour better change the above two methods to:

    Java Code:
    public int getTableLength() { return Table.length; }
    public int getTableElement(int i) { return Table[i]; }
    Note that you can't set/change individual array elements with these two methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why make class variables private?
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-18-2010, 12:01 PM
  2. Public, private or (nothing) class
    By tyang in forum New To Java
    Replies: 3
    Last Post: 02-01-2010, 12:37 AM
  3. declare class data member as an inteface type
    By everurz in forum Advanced Java
    Replies: 3
    Last Post: 12-21-2009, 02:04 PM
  4. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 10:05 PM
  5. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 10:08 PM

Posting Permissions

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