This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 4240
    
    7

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: 60049
    
  65

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: 60049
    
  65

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: 12675
    
    5
I think the OP was posting snipits of code from the GenericlServlet class - here is the full version from Tomcat 7.0.27



Java Resources at www.wbrogden.com
 
jQuery in Action, 2nd edition
 
subject: why Config variable declared as private in GenericServlet class.
 
Similar Threads
Why does GenericServlet implement ServletConfig
Regarding the init() methods
why sun people declared config variable as transient in generic class?
init() methods in Generic Servlet
init() method doubt?