# Zig structs vs Java classes

**URL:** <https://ziggit.dev/t/zig-structs-vs-java-classes/4637>\
**Category:** Explain\
**Tags:** language\
**Created:** [June 8, 2024, 12:38am UTC](https://ziggit.dev/t/zig-structs-vs-java-classes/4637 "2024-06-08T00:38:37Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![smlavine](https://ziggit.dev/letter_avatar_proxy/v4/letter/s/eb9ed0/32.png) [@smlavine](https://ziggit.dev/u/smlavine)\
**Post date:** [June 8, 2024, 2:21am UTC](https://ziggit.dev/t/zig-structs-vs-java-classes/4637/2 "2024-06-08T02:21:07Z")

</div>

Take care not to confuse what you’re calling “members” here and struct _fields_ though, which can just as well be declared at the top level and are _always public_:

```zig
//! A top-level module.

var inner: u32 = 0;
pub const foo: i32 = 123; // Yes, this needs `pub`...

// But this is a field, and all fields are public.
// Mind the comma.
number: u32,

pub fn init() @This() {
    const x: @This() = .{ .number = inner };
    inner += 1;
    return x;
}

```

```zig
// main.zig

const std = @import("std");
const Mod = @import("Module.zig");

pub fn main() void {
    const m = Mod.init();
    std.debug.print("m.number = {}\n", .{m.number});

    std.debug.print("next is {}\n", .{Mod.init().number});
    std.debug.print("next is {}\n", .{Mod.init().number});
    std.debug.print("next is {}\n", .{Mod.init().number});
    std.debug.print("next is {}\n", .{Mod.init().number});
}

```

```shell
$ zig run main.zig
m.number = 0
next is 1
next is 2
next is 3
next is 4

```

---

_[View the full topic](https://ziggit.dev/t/zig-structs-vs-java-classes/4637)._
