aspose file tools*
The moose likes Servlets and the fly likes why Config variable declared as private in GenericServlet class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why Config variable declared as private in GenericServlet class." Watch "why Config variable declared as private in GenericServlet class." New topic
Author

why Config variable declared as private in GenericServlet class.

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100

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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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

Joined: Jan 10, 2002
Posts: 61218
    
  66

There is no need at all to do this. The config is readily available from the Servlet superclass. Storing it is unnecesarry and amateurish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sourabh Bawage
Ranch Hand

Joined: Sep 03, 2013
Posts: 33

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....


SCJP
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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

Joined: Mar 22, 2000
Posts: 12785
    
    5
I think the OP was posting snipits of code from the GenericlServlet class - here is the full version from Tomcat 7.0.27


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Config variable declared as private in GenericServlet class.