title: How to create a "single dispatch, object-oriented Class" in julia that behaves like a standard Java Class with public / private fields and methods
body: <p>I read in a book that "you can't create traditional 'classes' in julia with single-dispatch-style methods like <code>obj.myfunc()</code>" ... and I thought that sounded more like a challenge than a fact.</p>
  
  <p>So here's my <code>JavaClass</code> type with public / private fields and methods just for the sheer shock and horror factor of having something ugly like this in Julia, after all the trouble the devs have gone to to avoid it:</p>
  
  
  
  <pre class="lang-rb prettyprint-override"><code>type JavaClass
  
      # Public fields
      name::String
  
      # Public methods
      getName::Function
      setName::Function
      getX::Function
      getY::Function
      setX::Function
      setY::Function
  
      # Primary Constructor - "through Whom all things were made."
      function JavaClass(namearg::String, xarg::Int64, yarg::Int64)
  
          # Private fields - implemented as "closed" variables
          x = xarg
          y = yarg
  
          # Private methods used for "overloading"
          setY(yarg::Int64) = (y = yarg; return nothing)
          setY(yarg::Float64) = (y = Int64(yarg * 1000); return nothing)
  
          # Construct object
          this = new()
          this.name = namearg
          this.getName = () -&gt; this.name
          this.setName = (name::String) -&gt; (this.name = name; return nothing)
          this.getX = () -&gt; x
          this.getY = () -&gt; y
          this.setX = (xarg::Int64) -&gt; (x = xarg; return nothing)
          this.setY = (yarg) -&gt; setY(yarg) #Select appropriate overloaded method
  
          # Return constructed object
          return this
      end
  
      # a secondary (inner) constructor
      JavaClass(namearg::String) = JavaClass(namearg, 0,0)
  end
  </code></pre>
  
  <p>Example use:</p>
  
  <pre class="lang-rb prettyprint-override"><code>julia&gt; a = JavaClass("John", 10, 20);
  
  julia&gt; a.name # public
  "John"
  
  julia&gt; a.name = "Jim";
  
  julia&gt; a.getName()
  "Jim"
  
  julia&gt; a.setName("Jack")
  
  julia&gt; a.getName()
  "Jack"
  
  julia&gt; a.x # private, cannot access
  ERROR: type JavaClass has no field x
  
  julia&gt; a.getX()
  10
  
  julia&gt; a.setX(11)
  
  julia&gt; a.getX()
  11
  
  julia&gt; a.setY(2) # "single-dispatch" call to Int overloaded method
  
  julia&gt; a.getY()
  2
  
  julia&gt; a.setY(2.0)
  
  julia&gt; a.getY()  # "single-dispatch" call to Float overloaded method
  2000
  
  julia&gt; b = JavaClass("Jill"); # secondary constructor
  
  julia&gt; b.getX()
  0
  </code></pre>
  
  <p>Essentially, the constructor becomes a closure, which is how one creates "private" fields and methods / overloading.
  Any thoughts? (other than <em>"OMG Why??? Why would you do this??"</em>)<br>
  Any other approaches?<br>
  Any scenarios you could envisage where this might fail spectacularly?</p>