Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Public members VS access methods
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Aziz




PostPosted: Wed Aug 02, 2006 3:29 pm   Post subject: Public members VS access methods

While working on my project, I've noticed it is a pain, and seemingly pointless, to use get() and set() methods when all they contain is return value; or this.value = value; instead of a one public int value;

It hurts me deep especially when there's about 7 of these guys. It is recommended to use access methods, because they provide control over how other class access those variables. For example, say you want to change a cost of an object, you would have a setCost(double cost) that would make sure a negative value isn't put in. But honestly, what if you don't care?
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Aug 02, 2006 8:20 pm   Post subject: (No subject)

These access methods are usually used to protect your program from being tampered with. The rpg game you were making, for instance, access methods would prevent others from messing around with your values, i.e. hacking. I suppose you could start off not using access methods until you've finished your program, but I did that with my FP, and it was really annoying at the end to change everything up. But, if you don't care, then I guess you don't have do it.
Aziz




PostPosted: Wed Aug 02, 2006 10:34 pm   Post subject: (No subject)

That's true. I'm actually sticking to it and using access methods (inheritence helps with this) and I'm writing javadoc for my classes, too. Tedious, but it pays off.
NikG




PostPosted: Wed Aug 02, 2006 10:49 pm   Post subject: (No subject)

I was wondering this too (but in Turing, not Java).

I recently went and coverted the 1500+ lines of my RTS game to use classes and that was a royal pain in the ass. Was it worth it? I'm still trying to decide that.

My 2 cents: I believe that if you can program your... er... program carefully, than the risk of problems with public members is taken care of. If you fear that there is risk with the variables being accidentally changed, than access methods are necessary.

Question: is it recommended to use both Get() and Set() for variables? I am currently only use Set() because I figure you can just use the variable itself for the Get().
wtd




PostPosted: Thu Aug 03, 2006 2:36 am   Post subject: (No subject)

If all you are doing is:

code:
class Pointless {
   private int field;

   public int getField() { return field; }
   public void setField(int field) { this.field = field; }
}


Then yes, accessors would indeed seem like one of the stupider ideas to come along. Where they become more meaningful is if we were to do something like:

code:
class SlightlyLessPointless {
   private int field;

   public int getField() { return field; }

   public void setField(int field) {
      if (field > this.field)
         this.field = field;
   }
}
HellblazerX




PostPosted: Thu Aug 03, 2006 7:26 am   Post subject: (No subject)

NikG wrote:
Question: is it recommended to use both Get() and Set() for variables? I am currently only use Set() because I figure you can just use the variable itself for the Get().

If you're using the Set() method, then I'm assuming you've declared that variable to be private. In that case, you'll need the Get() method, because you won't have direct access to that variable. If the variable is set to private, it means you can't access it at all (from outside that class that is).
Aziz




PostPosted: Thu Aug 03, 2006 9:45 am   Post subject: (No subject)

HellblazerX wrote:
NikG wrote:
Question: is it recommended to use both Get() and Set() for variables? I am currently only use Set() because I figure you can just use the variable itself for the Get().

If you're using the Set() method, then I'm assuming you've declared that variable to be private. In that case, you'll need the Get() method, because you won't have direct access to that variable. If the variable is set to private, it means you can't access it at all (from outside that class that is).


Also: You might want to control what the user recieves. You may want to return a different value depending on someting.
NikG




PostPosted: Thu Aug 03, 2006 2:21 pm   Post subject: (No subject)

HellblazerX wrote:
NikG wrote:
Question: is it recommended to use both Get() and Set() for variables? I am currently only use Set() because I figure you can just use the variable itself for the Get().

If you're using the Set() method, then I'm assuming you've declared that variable to be private. In that case, you'll need the Get() method, because you won't have direct access to that variable. If the variable is set to private, it means you can't access it at all (from outside that class that is).
But I haven't set my variable to private, just so that I can access it directly instead of creating a Get(). Now I know, that's "dangerous", but that's where my question really comes in... if I'm certain that I programmed carefully enough such that there's never an attempt to change the variable directly, isn't what I did sufficient?
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Thu Aug 03, 2006 2:50 pm   Post subject: (No subject)

get/set are java bean conventions. Since it is also a standard java convention, you should implement them. It would be confusing to other programmers when they try and modify/extend your code.

Note: Although get/set are java beans conventions they do not make up a java bean. A real java bean is a complete component. The swing components are all real java beans. Not only do they have these get/set methods but they have property listeners etc.. Which are pretty cool btw.
Aziz




PostPosted: Thu Aug 03, 2006 11:10 pm   Post subject: (No subject)

A little off topic, but rizzix, you should really right a bean tutorial. I haven't the slightest (okay, well maybe the slightest) clue what they are or why they exist.
wtd




PostPosted: Fri Aug 04, 2006 10:12 am   Post subject: (No subject)

Heh, rizzix, one reason to like Ruby is that it forces encapsulation. Instance variables cannot possibly be anything other than private. Smile

That said, it also makes it really easy to write accessors if you're determined to provide them.
Aziz




PostPosted: Sat Aug 05, 2006 8:09 am   Post subject: (No subject)

If I'm not mistaken, isn't the default (no access modifier, like 'String name') package access, not public?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: