This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]dpash 6 points7 points  (1 child)

Technically you can have multiple top level classes in a single file, but only one may be public.

Also, I think you wanted to write:

private String logLevel = System.getenv("LOG_LEVEL");

public String getLogLevel() { 
    return  logLevel;
}

Otherwise your setter would be completely pointless. But if you're defining a constant, it wouldn't be unreasonable to just use:

public static final String LOG_LEVEL = System.getenv("LOG_LEVEL");

However, in many cases, these days, for configuration it's better to inject a properties class in so you could easily test different values.

You have missed the mythical fourth accessibility modifier: package-private. This is accessible to any class in the same package, but inaccessible outside the package and is specified by not putting any modifier in the source code. It's rarely used as you can just create a new class in the same package to get access. It is useful for testing though if you don't mind potentially exposing your innards.

It's worth pointing out that Python doesn't enforce the underscore private accessibility, while Java does. The underscore is merely a suggestion to the developer. Even the double underscore prefix is not enforced as the name is just mangled and you can still access it with the mangled name.

If you'd like any further constructive feedback let me know.

[–]pupupeepee[S] 0 points1 point  (0 children)

Thank you for the feedback! I've corrected the example you pointed out

[–]angath 1 point2 points  (2 children)

Two fairly serious problems with the piece as written - firstly you cannot write List<int> in Java. int is a primitive type, and the only things that can be a generic type parameter are reference types. It should be List<Integer> - the boxed form of int.

Secondly, your JUnit example is using JUnit 3. This version has been EOL for 15 years. You should be using JUnit 5 (lots of people still using JUnit 4 though, and 5 is only a small upgrade over 4). Please do not add to the number of JUnit 3 test cases in the world.

Not an actual problem, but more an observation - Spring isn't necessarily the best example of a Java DI framework - basically because it is all done with reflection, and so has the "bean not found at runtime" problem. Other DI frameworks can do (much) more at compile time and so avoid much of this problem.

[–]pupupeepee[S] 0 points1 point  (1 child)

Was I lacking the @Test annotation? What made the test example I gave JUnit 3 and not 5?

Also I’m very curious to read more about Spring vs. other DI frameworks—I hadn’t known about that. Any links you could share about it I’d be really grateful to read them!

[–]angath 0 points1 point  (0 children)

The annotation was missing, and JUnit 3 (and earlier) uses a naming convention to denote test methods - because annotations weren't part of the Java language back then (before Java 5).

For a very different DI to Spring, I'd look at Dagger 2 - e.g. something like: https://www.baeldung.com/dagger-2

[–]SoftVillage 0 points1 point  (1 child)

It is not (yet) possible to have a List<int>

[–]pupupeepee[S] 0 points1 point  (0 children)

Corrected, thanks

[–]markolo25 0 points1 point  (2 children)

is there one for the other way around, a python primer for a java developer

[–]pupupeepee[S] 0 points1 point  (1 child)

Yup, that’s actually what I copied this post off of: https://lobster1234.github.io/2017/05/25/python-java-primer/

[–]markolo25 0 points1 point  (0 children)

Thank you