# Basic class code

**URL:** <https://forum.crystal-lang.org/t/basic-class-code/6222>\
**Category:** Help & Support\
**Created:** [December 16, 2023, 3:33pm UTC](https://forum.crystal-lang.org/t/basic-class-code/6222 "2023-12-16T15:33:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![e4Nf6](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@e4Nf6](https://forum.crystal-lang.org/u/e4Nf6)\
**Post date:** [December 16, 2023, 3:33pm UTC](https://forum.crystal-lang.org/t/basic-class-code/6222/1 "2023-12-16T15:33:03Z")

</div>

Hi there, i’m back on Crystal and i’ve a basic question: what is the difference between these two class declarations?

1. 

```
class Person
   @name : String
   @age : Int32

  def initialize(name, age)
  @name = name
  @age = age
 end

def name
@name
end

 def age
  @age
 end
 end

```

2. 

```
 class Person
    def initialize(name : String, age : Int32)
    @name = name
    @age = age
   end

def name
@name
end

def age
@age
 end
end

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [December 16, 2023, 3:37pm UTC](https://forum.crystal-lang.org/t/basic-class-code/6222/2 "2023-12-16T15:37:37Z")

</div>

They’re functionally the same. Compiler knows the type of the two instance variables in the first example because they’re declared explicitly. In the second example it can infer it from the parameter type restrictions.

Tho I would say the more idiomatic approach would be like:

```crystal
class Person
  getter name : String
  getter age : Int32

  def initialize(@name : String, @age : Int32); end
end

```

---

<div class="post-metadata">

**Author:** ![e4Nf6](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@e4Nf6](https://forum.crystal-lang.org/u/e4Nf6)\
**Post date:** [December 16, 2023, 3:44pm UTC](https://forum.crystal-lang.org/t/basic-class-code/6222/3 "2023-12-16T15:44:11Z")

</div>

Ok, thx you 👌
