#Private fields Java

1 messages · Page 1 of 1 (latest)

wispy viper
#

Am I misunderstanding how private fields work in Java? I was under the impression once a variable is marked as private any other code can not access it, re-assign it, etc.

class EvenNumberHolder {
    private int value;

    EvenNumberHolder(int value) {
        if (value % 2 == 1) {
            throw new RuntimeException(value + " is not even");
        }

        this.value = value;
    }
}

void main() {
    EvenNumberHolder enh = new EvenNumberHolder(20);

    IO.println(enh.value);
    enh.value = 22;
    IO.println(enh.value);
}

When I re-assign the variable to a new value it updates?

tribal jacinthBOT
#

<@&987246399047479336> please have a look, thanks.

summer notch
#

you are using a compact source file so your EvenNumberHolder is actually a inner class of the implicit outer class that gets added by the compiler. And the outer class has access to the private members of the inner class because the whole class is a member of that class

wispy viper
wispy viper
#

Ok, I see what's going on lol. Silly mistake but good to know. Thanks!

ripe quail
#
class Main {
  ...

  void main() {
    ...
  }
}
#

essentially

summer notch
#

i think when you want to use classes it is the best to not use compact source files

#

use it for small programs which you would write in other languages with only functions

wispy viper
#

Yea exactly, Ok. Counter question does every file have a "hidden" Main class or does this only reside in the file named Main.java?

summer notch
#

compact source files are the only files where a implicit class is added

wispy viper
summer notch
wispy viper
#

Ah ok, this is why in older Java videos, lessons, etc. you would see things like

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
summer notch
#

it is still used in java 25 and java 26, compact source files are just for small programs, and for learners at the beginning, not to replace javas OOP design or to be used always. It is an addional way, not the new way

#

Have your classes as a public class in file per class and then you import them and use them in your main file

low yarrow
wispy viper
#

Awesome, thank you!

low yarrow
#
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
#

Because the public on the class itself was always optional

wispy viper
#

so by default if the public is omitted its still public anyway unless specified?

low yarrow
#

But you already had to write it on the old main method, people cargo culted it in a lot of places, ... So people just slapped it on there

#

No, you'll see when you get to visibility modifiers but public means "visible to all code in all packages"

#

No modifier means "package private," so "visible to all code in the same folder" (more or less)

wispy viper
#

So to just re-itterate to make sure I understand, the code above that you placed is considered "package private" where only code within the same directory (folder) can gain access too it.

So if this is in a class folder, other files within the class folder/directory can access HelloWorld but anything outside of that folder can not. In order for other files to get access you would need to add public?

low yarrow
#

Eh you'll understand when you get to packages

wispy viper
#

😂 Sounds good, appreciate it as always

ripe quail
#

ur on the right track though

#

like, it wont fully click now but ur idea of it sounds about right

wispy viper
#

Ok, I'm trying to draw similarities to how things are done using JS, such as import/export and the very "loose" class system in JS which is basically just syntactic sugar lol

low yarrow
#

The defaults in Java is "export-ish"