12 Replies - 860 Views - Last Post: 16 April 2012 - 03:18 PM Rate Topic: -----

#1 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Private Instance variable and public instance method?

Posted 16 April 2012 - 12:53 PM

Hi there,

Why would the following: If a class has a private instance variable declared as: private ArrayList<H> h;
and a public instance method with the statement: return h; be a problem??
Is This A Good Question/Topic? 0
  • +

Replies To: Private Instance variable and public instance method?

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 12:54 PM

No problem there. What you describe is a simple 'getter' method that are used all the time.
Was This Post Helpful? 0
  • +
  • -

#3 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 12:55 PM

View PostRyano121, on 16 April 2012 - 12:54 PM, said:

No problem there. What you describe is a simple 'getter' method that are used all the time.


Yes, but would it not be a 'design problem'??
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 12:58 PM

What do you mean by problem? Do you mean "Why could it be a design/architecture flaw?" or "Why would it fail to compile/run as intended?"?

If you mean the latter: it wouldn't. If it doesn't compile or run as intended for you, you did something wrong somewhere.

If you mean the former: returning the list like that allows the caller to modify the list (e.g. add or remove items to/from it), which might not be intended if you only provide a getter, but no setter with the intention that the list can not be modified from the outside.
Was This Post Helpful? 2
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:00 PM

It would only be a potential 'design problem' if the private variable was only used by that one class and so there would be no point in allowing public access to it.

If however the private variable was an ArrayList then you could have potential problems. For example by allowing public access to the whole object, and not its contents, you allow users to simply.

theobject.getList().clear();


Which may very well mess up the operation of the class.

It would be better to provide a method to get the member at an index, or something to that effect depending on the purpose of the class

public H getHAt(int index)
{
    return h.get(index); // also include error handling if the index was out of bounds
}

Was This Post Helpful? 2
  • +
  • -

#6 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:05 PM

View PostRyano121, on 16 April 2012 - 01:00 PM, said:

It would only be a potential 'design problem' if the private variable was only used by that one class and so there would be no point in allowing public access to it.

If however the private variable was an ArrayList then you could have potential problems. For example by allowing public access to the whole object, and not its contents, you allow users to simply.

theobject.getList().clear();


Which may very well mess up the operation of the class.

It would be better to provide a method to get the member at an index, or something to that effect depending on the purpose of the class

public H getHAt(int index)
{
    return h.get(index); // also include error handling if the index was out of bounds
}


Thank you so much for your answer - that clarified my confusion. But, just one last thing if I changed the instance method to be private would that remove the 'potential flaws' ???
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:06 PM

Well yes but there would be no purpose in the method. Members inside the class could just refer to the variable itself instead of going through a private getter method.
Was This Post Helpful? 1
  • +
  • -

#8 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:06 PM

If you changed it to private, there'd be no point to it. Everybody who'd be allowed to call the instance method, would also be allowed to access the variable directly (so why bother with calling the method?).
Was This Post Helpful? 0
  • +
  • -

#9 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:09 PM

View Postsepp2k, on 16 April 2012 - 01:06 PM, said:

If you changed it to private, there'd be no point to it. Everybody who'd be allowed to call the instance method, would also be allowed to access the variable directly (so why bother with calling the method?).


Oh I see, so what can I add or alter to remove the potential problem?
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,707
  • Joined: 19-March 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:12 PM

Access modifiers control direct access to a field. All fields should be private by default, and only grudgingly granted wider access.
You can then use methods to grant more controlled access at your whim, if you like. You should think twice about creating getters and setters, though - the design issue for me is, why does some other variable need to know the details of this class's innards? Generally, I find that good design eliminates the need for this.

But no, public read access to a field through a getter does not in itself compromise a variable's "private" status.
Was This Post Helpful? 1
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:14 PM

View PostRuru123, on 16 April 2012 - 10:09 PM, said:

Oh I see, so what can I add or alter to remove the potential problem?


View PostRyano121, on 16 April 2012 - 10:00 PM, said:

It would be better to provide a method to get the member at an index, or something to that effect depending on the purpose of the class

public H getHAt(int index)
{
    return h.get(index); // also include error handling if the index was out of bounds
}

Was This Post Helpful? 1
  • +
  • -

#12 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 01:15 PM

Thanks all. :)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Private Instance variable and public instance method?

Posted 16 April 2012 - 03:18 PM

View PostRuru123, on 16 April 2012 - 03:55 PM, said:

View PostRyano121, on 16 April 2012 - 12:54 PM, said:

No problem there. What you describe is a simple 'getter' method that are used all the time.


Yes, but would it not be a 'design problem'??

No it's the way it works and it was designed to work that way. Why ? So what pass internally of the class is actually private to the class.

Imagine a situation where uou have a class Item and a method that returns a total price

class Item {
   private int quantity;
   private double price;

   public Item(int quantity, double price) {
      this.quantity = quantity;
      this.price = price;
   }

   public double getTotalPrice() {
      return quantity * price;
   }



now, later on, for whichever reasons, you decide to calculate the totalPrice at construction time

class Item {
   private int quantity;
   private double price;
   private double total;

   public Item(int quantity, double price) {
      this.quantity = quantity;
      this.price = price;
      total = price * quatinty;
   }

   public double getTotalPrice() {
      return total;
   }



The callers of getTotalPrice() will not have a clue that the way Item manages its total price has changed. The interanl way of calculating the total price is private to class Item.

If quantity and price were not private the user may have written

double total = item1.price * item1.quantity;

and what if you decide to add taxes

   public double getTotalPrice() {
      return quantity * price * 1.05;   // add 5% tax
   }



Your user would be screwed up, his formula wouldn't work anymore. By forcing him to call getTotalPrice() you avoid that problem.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1