Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @Nullable in Java

Tags:

java

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).

like image 535
Slimu Avatar asked Apr 10 '14 12:04

Slimu


People also ask

What does @nullable do in Java?

@Nullable The @Nullable annotation helps you detect: Method calls that can return null. Variables (fields, local variables, and parameters), that can be null.

How do you make a nullable in Java?

you can use : Boolean b = null; that is, the java. lang.

Does Java have NULL safety?

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.


1 Answers

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

like image 159
kAnNaN Avatar answered Sep 21 '22 07:09

kAnNaN