hadoop-mapreduce-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ted Dunning <tdunn...@maprtech.com>
Subject Re: Static class vs Normal Class when to use
Date Thu, 28 Mar 2013 07:54:46 GMT
Another Ted piping in.

For Hadoop use, it is dangerous to use anything but a static class for your
mapper and reducer functions since you may accidentally think that you can
access a closed variable from the parent.  A static class cannot reference
those values so you know that you haven't made that mistake.  If the mapper
or reducer class is not embedded in another class (i.e. not a member class)
then it is automatically static since there is nothing to inherit from.

Contrarily, static members in the mapper or reducer are real trouble unless
they are static final.  The problem is that different mapper and reducer
invocations will see different values of these static values because they
are running in different JVM's.  Dependence on statics is a common
beginner's mistake in Hadoop.

On Thu, Mar 28, 2013 at 6:01 AM, Sai Sai <saigraph@yahoo.in> wrote:

> Hi Ted
> Thanks for your help it is really appreciated.
> I have gone thru the links that you have provided but i am looking for a
> simple exaplanation of using static classes in Hadoop/MR while
> syntactically i know how the static classes and variables r referred, i am
> wondering if there was something more than that we use static classes.
> In other words assuming static classes r loaded at the time the classes r
> loaded and not at the time the object is created and the timespan of
> objects of static classes r less than normal classes. Please let me know if
> this make sense and provide any additional info if you could.
> Thanks again.
> Sai
>   ------------------------------
> *From:* Ted Yu <yuzhihong@gmail.com>
> *To:* user@hadoop.apache.org; Sai Sai <saigraph@yahoo.in>
> *Sent:* Thursday, 28 March 2013 9:41 AM
> *Subject:* Re: Static class vs Normal Class when to use
> Take a look at Effective Java 2nd edition:
>  Item 22: Favor static member classes over nonstatic
> On Wed, Mar 27, 2013 at 9:05 PM, Ted Yu <yuzhihong@gmail.com> wrote:
> See
> http://stackoverflow.com/questions/1353309/java-static-vs-non-static-inner-class
> I believe Josh Bloch covers this in his famous book.
> On Wed, Mar 27, 2013 at 9:01 PM, Sai Sai <saigraph@yahoo.in> wrote:
> In some examples/articles sometimes they use:
> public static class MyMapper
> and sometimes they use
> public class MyMapper
> When/why should we use static vs normal class.
> Thanks
> Sai

View raw message