# Classes in Julia

**URL:** <https://discourse.julialang.org/t/classes-in-julia/24521>\
**Category:** General Usage\
**Tags:** mutable-structure, multidispatch, constructors\
**Created:** [May 23, 2019, 2:31pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521 "2019-05-23T14:31:11Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [May 23, 2019, 2:31pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/1 "2019-05-23T14:31:11Z")

</div>

How do we create classes in Julia that involves the **init** part in Python?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 23, 2019, 2:40pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/2 "2019-05-23T14:40:31Z")

</div>

Julia does not have classes in the object-oriented sense as it is not an object-oriented language. The main paradigm of Julia which replaces object-oriented programming is [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch), see [here](https://docs.julialang.org/en/v1/manual/noteworthy-differences/).

That said, the closest analogy to a Python `class` is a `mutable struct` (though you should default to using `struct` when possible). The biggest difference between these and a Python class is that in almost al use cases these do not “own” methods, only data. These are initialized with [constructors](https://docs.julialang.org/en/v1/manual/constructors/) which are analogous with the Python `init` method.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 23, 2019, 4:05pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/3 "2019-05-23T16:05:36Z")

</div>

> [@ExpandingMan](#):
>
> Julia does not have classes in the object-oriented sense as it is not an object-oriented language.

Out of curiosity. What exactly does it mean to say that Julia is not object-oriented?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [May 23, 2019, 4:14pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/4 "2019-05-23T16:14:31Z")

</div>

big question.

A language that is designed to be “object-oriented” is, at its philosophical and pragmatic core, a playground for sculpting with your own or other persons’ clay. Objects are empty until they become designed with purposeful capabilities; and then they become both coin and cloth of the computational relm. Objects are computational aor user interactive wellsprings; the water is good.

Julia is its own motive experience, one that continues to unfold and advance. We write software more as expressive design, expertly informed and easily realized. The entities that compute, cooperate – how? In Julia, dispatch over types is really multidispatch over kinds of types and guided placement of realizable intent.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 23, 2019, 4:33pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/5 "2019-05-23T16:33:17Z")

</div>

> [@e3c6](#):
>
> Out of curiosity. What exactly does it mean to say that Julia is not object-oriented?

I take it to mean that methods are not owned by classes.

I don’t claim any formal computer science knowledge, and I can imagine the distinction being rather hazy snice, for example, `struct`s certainly _can_ own methods in any language with multiple dispatch and first-class functions.

As a practical matter, one of the biggest differences between writing Julia code and writing Python code is simply that you don’t define functions within `struct`s, nor are functions necessarily associated with specific `struct`s when you call them. If you come to Julia from C++ like I did, this immediately seems like a _very_ big difference (and, in my opinion, one is infinitely better off in the Julia case).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 23, 2019, 4:46pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/6 "2019-05-23T16:46:20Z")

</div>

> [@e3c6](#):
>
> Out of curiosity. What exactly does it mean to say that Julia is not object-oriented?

Mostly that it doesn’t have classes. Despite classes having no place in Alan Kay’s original conception of object oriented programming, but these days when people say “object oriented” they mean “like C++, Java and Python” 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [May 23, 2019, 4:47pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/7 "2019-05-23T16:47:13Z")

</div>

Can someone maybe give an example of how these **structs** and **types** work assuming that you have to define the \_\_init\_\_part in a class in python

Thank you

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [May 23, 2019, 4:50pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/8 "2019-05-23T16:50:34Z")

</div>

It’s unclear (to me, at least) quite what you’re asking here. Do you want to know how to construct instances of types in Julia? Using ` __init__ ` in Python as a reference point may not be helpful since that’s not how things work in Julia. Have you read the section on [composite types](https://docs.julialang.org/en/v1/manual/types/#Composite-Types-1) (aka “structs”) in the Julia manual? If so, was there something in there that was unclear?

---

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [May 23, 2019, 4:58pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/9 "2019-05-23T16:58:34Z")

</div>

Take for example this python code, how can you translate to Julia

class Employee:  
empCount = 0

def **init** (self, name, salary):  
self.name = name  
self.salary = salary  
Employee.empCount += 1

def displayCount(self):  
print(“Total Employee %d” % Employee.empCount)

def displayEmployee(self):  
print("Name : ", self.name, ", Salary: ", self.salary)

emp1 = Employee(“Zara”, 2000)  
emp2 = Employee(“Manni”, 5000)

emp1.displayEmployee()  
emp2.displayEmployee()

print(“Total Employee %d” % Employee.empCount)

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [May 23, 2019, 5:06pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/10 "2019-05-23T17:06:30Z")

</div>

I always liked this blog and I think it can help people understand the difference with an actual example.

> **[Defining Custom Units in Julia and Python](https://erik-engheim.medium.com/defining-custom-units-in-julia-and-python-513c34a4c971)**
>
> I’ve been writing code to deal with temperature in different units Celsius, Kelvin and Fahrenheit, because I do calculations involving the…

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 23, 2019, 5:10pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/11 "2019-05-23T17:10:24Z")

</div>

Perhaps something like this

```julia
const GLOBALS = Dict(:empCount=>0)
                  
                   
struct Employee    
    name::String   
    salary::Float64
   
    function Employee(name::AbstractString, sal::Real)
        GLOBALS[:empCount] += 1
        new(name, sal)
    end
end

display_employee_count() = println("Total Employees: $(GLOBALS[:empCount])")
# could also do
# display_count(::Type{Employee})

function Base.show(io::IO, emp::Employee)
    println(io, "Employee (Name: $(emp.name), Salary: $(emp.salary))")
end
   

emp1 = Employee("Zara", 2000)
emp2 = Employee("Manni", 5000)

show(emp1)
show(emp2)

display_employee_count()

```

Note that there are lots of options for dealing with the global variable `Employee.empCount` (as it’s called in your Python code). Ideally, I’d recommend eliminating this global. You probably can: would you perhaps like to put the `Employee`s in a `Vector`? That’s just one example of a data structure that might contain or reference the `Employee`s which maintains a count for you. You also don’t necessarily have to use `Base.show`, you could just define a new function for `display_employee` (with or without the `IO` argument), but `Base.show` will be called any time Julia wants to display an `Employee` (for example, just do `emp1` in the REPL after the above).

Again, lots of ways of doing much of this, but the above examples is very close to your Python code.

---

<div class="post-metadata">

**Author:** ![kirui](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirui/32/8585_2.png) [@kirui](https://discourse.julialang.org/u/kirui)\
**Post date:** [May 23, 2019, 5:14pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/12 "2019-05-23T17:14:57Z")

</div>

So basically you can write a function inside structs? In a case where I would like to eliminate the global, how will that be possible?  
Am still new to julia so I would like to see a simple code without the Base.show.  
Thanks for your enlightenment.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 23, 2019, 5:23pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/13 "2019-05-23T17:23:09Z")

</div>

I recommend you spend some time reading the documentation, in particular the section on [constructors](https://docs.julialang.org/en/v1/manual/constructors/). Constructors are “special” functions in that they are the only functions that can be defined within a struct. Again, you’ll need to read the documentation to understand why.

It’s hard to say how exactly you’d elminate the global without more context. Like I said, I’d normally expect your `Employee`s to be stored in some other data structure (perhaps just a `Vector`) that counts them for you. This would not be any different than in Python by the way, we just tend to be a bit more performance conscious in Julia. Nothing will “go wrong” if you do use a global, it just likely isn’t the best choice.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 20, 2021, 3:19pm UTC](https://discourse.julialang.org/t/classes-in-julia/24521/14 "2021-11-20T15:19:29Z")

</div>

3 posts were split to a new topic: [Who invented object oriented programming?](https://discourse.julialang.org/t/who-invented-object-oriented-programming/71830)
