• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which's the good practice

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,

I am modelling the game for cricket for a small OOP assignment.
I have the following classes:
GameManager -- to manage the game & to play -- the main controller
Batsman -- the whole purpose is to bat
Bowler -- the whole purpose is to make a random delivery

In the GameManger class, I have some variables like totalRuns, noOfWickets and a lot... These variables are not needed by any other classes according to my design. So, is it necessary to code the accesser methods(getters & setters) for these private variables? If I am coding them, do I need to make them as public? ( I'm not using these variables outside this class) & when I am using these variables inside the same class do I need to use the accessor methods or can I directly accesses them like 'this.totalRuns' ?

Also please clarify the purpose of using 'this' keyword when referring to the variables & methods of the same class. Is it necessary to use 'this' keyword always when I'm accessing the variables & methods inside the same class?

The purpose of this assignment is only "Object Orientation Concepts". Not the logic.
So, please help me with this.

Thanks in advance...

Usha
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, is it necessary to code the accesser methods(getters & setters) for these private variables?



It would not be necessary. However, it is good coding practice. I would expect these methods to be in the class and would direct the programmers to add them if they left them out. They are called accessors and mutators.

If I am coding them, do I need to make them as public? ( I'm not using these variables outside this class)



The variables do not need to be public. I would expect them to be private and would direct the programmers to make them private and add the accessor and mutator methods to the class.

when I am using these variables inside the same class do I need to use the accessor methods or can I directly accesses them like 'this.totalRuns'?



It is ok to directly access the variables within the class. The purpose of the accessor and mutator methods are to allow client objects to either get the values or ask the owning object to set the values.

Is it necessary to use 'this' keyword always when I'm accessing the variables & methods inside the same class?



It is not necessary to use the 'this' keyword. The class knows about it's own variables. However, it is a good programming practice to use the 'this' keyword. This improves readability and enables others that have not programmed the class to understand it faster, especially useful for classes with many methods and fields.

Good luck with your learning! Sounds fun
 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much James Clark for your immediate reply with the complete explanation which I wanted.
Thank you very much for your encouraging words.
One more question:
If the controller class's variables are not used outside that class, is it necessary to have the accessors and mutators?
If I am coding the accessors and mutators, what should be it's access modifier? ( Since the accessors and mutators are not accessed outside the class, is it ok to make them private?)
I am ok with using accessors and mutators for the bean class. But I want the explanation in the context of a 'Controlling' class.

Thanks in advance.

As a student, I'm very lucky to learn many things through this forum.
Thanks a lot.

With Regards,
Usha
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Usha,
There is no need to use any getter - setter 's if you are not gonna use it outside your class [& you should avoid using outside class]. look for [URL=
http://www.google.co.in/url?sa=U&start=1&q=http://en.wikipedia.org/wiki/Law_of_Demeter&ei=hNJESYuFLNKukAX41qS3DQ&usg=AFQjCNFVg3IgwFW2ASybTSECrrMCLpKj5Q] Law of demeter [/url]

And it is not good practice to make all the member variable's having getter-setter. It's an odd way of breaking encapsulation.


Cheers,
Nachiket
 
Usha Pnatha
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Nachiket Patel,

Thank you very much for your reply with that useful link...
It helped me to clear my doubts... Thanks a lot...

With Regards,
Usha
reply
    Bookmark Topic Watch Topic
  • New Topic