I have a question regarding the usage of @Nullable
annotation in Java.
From what I've read, it's a good practice to set in on methods that may return a null value. In this way, the IDE may help detect some null pointer exception errors or suggest the removal unnecessary null pointer exception checks if @NotNull
is used.
So far so good, but what about using @Nullable
for method parameters? Is this a good practice or will the code become even more verbose (if used with final
) and the benefit may be missing since you don't always know what arguments will be passed to the function call? Also, what is your opinion on using @Nullable
with setter methods?
The usage is related to a situation when working in a company and not to a small project (like a homework).
@Nullable The @Nullable annotation helps you detect: Method calls that can return null. Variables (fields, local variables, and parameters), that can be null.
you can use : Boolean b = null; that is, the java. lang.
The reason is that Java does not support null safety, and non-nullable types do not exist. In other terms, any variable is always a nullable reference and there is no way to avoid null values except with custom logic. Thus, any reference in Java may be null by default.
Due to the inherent complexity, flow analysis is best performed in small chunks. Analyzing one method at a time can be done with good tool performance - whereas whole-system analysis is out of scope for the Eclipse Java compiler. The advantage is: analysis is fast and can be done incrementally such that the compiler can warn you directly as you type. The down-side: the analysis can not "see" which values (null or non-null) are flowing between methods (as parameters and return values).
This is where null annotations come into play. By specifying a method parameter as
@NonNull
you can tell the compiler that you don't want a null value in this position.
Reference
Reference 2
Usage: This link explains what annotation to use where.
Usage 2
Getters/Setters
: Yes, it is possible. The Project Lombok (http://projectlombok.org/index.html) defines annotations for generating getters/setters and more.So for example
@lombok.Data; public class Person { private final String name; private int age; }
Will generate getter for name (not setter since it is final) and getter/setter for age. It will also generate
equals, hashCode, toString
and construtor initializing required fields (name). Adding@AllArgsConstructor
would generate constructor initializing both fields.There are other annotations and parameters giving you control over access rights (should your getter be protected or public), names (getName or name?), etc. And there is more. For example, I really like the extension methods.
Lombok is very easy to use. Just download the jar and use the annotations, then the getter/setters can be used in your code without actually being spelled out. Moreover, IDE's like Netbeans support this, so that you see the getter/setter in code completion, navigation, etc. The annotations are used only during compilation not during runtime, so you don't distribute lombok with your jar's.
NotNull: This is supported by findbugs and IdeaJ IDE, maybe others
Reference 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With