Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why Config variable declared as private in GenericServlet class.

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

my question is that why developer declared config reference variable in GenericServlet class as private and he gave getServletConfig() to retrieve ServletConfig object.


 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it's generally good practice to make all instance variable private and access them via methods. This is information hiding - you're separating the implementation of the class from its public interface. This reduces coupling, and makes your code much more flexible. Maybe you'll never need the flexibility in this case, but you can never tell in advance where you will need the flexibility. So it's better to get into the habit of doing it right all the time.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need at all to do this. The config is readily available from the Servlet superclass. Storing it is unnecesarry and amateurish.
 
Sourabh Bawage
Ranch Hand
Posts: 33
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There is no need at all to do this. The config is readily available from the Servlet superclass. Storing it is unnecesarry and amateurish.


I didnt get what you are trying to say....

1. I think the config is declared in GenericServlet class.
2. Storing it is unnecesarry and amateurish.....what does it mean???

Could you please explain sir.....

Thanks....
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64683
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The OP is copying the config to his own variable. This is not necessary and not advised.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the OP was posting snipits of code from the GenericlServlet class - here is the full version from Tomcat 7.0.27


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic