Passing null as parameter in java




















Manish Khurana. Optional 'thank-you' note:. I thought it had to do with null being fitted into more specific String rather than Object. Ulf Dittmer. You're right on both parts. Rob Spoor. The outcome of passing null as a parameter is independent of the actions of the method for which it is an argument, but instead depends on the ability of the return datatype to deliver a null value.

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable I and variable A are null, since values have not been assigned to them.

When you learn how to design APIs, learn from people with greater experience. The majority of developers agree that when the list of method parameters grows too long it become hard to read. Usually, you handle the issue with the Parameter Object pattern. The parameter object is a named container class which groups all method parameters. However, when the list of constructor parameters is getting longer and many of them are optional parameters, applying constructor overloading may seem cumbersome.

If you created a constructors to cover all possible combination with optional parameters, you would end up with a quite overwhelming list. You usually implement the builder as an inner class of the class it suppose to build.

That way, both classes have access to their private members. Instead of a public constructor, we only expose one single static factory method for the inner builder class. The private constructor which the builder calls in the build method uses a builder instance to assign all fields and verifies if all required values are present. The client code of that builder which sets only a selected optional parameter may look as follows:.

With the builder, you can create all possible combinations with optional parameters of the object. What is more, without knowing you can omit the required parameters by accident. You need to slightly modify the builder factory method so that you can call it only with required parameters and left builder methods only for optional parameters. All popular Java IDEs have plugins which allow to generate class builders. You can also find alternative plugins in the official repositories.

If you use the project Lombok , it also simplify working with class builders. You can check this short introduction to Lombok builders if you need a place to start.

While browsing the web in search for approaches to work with Java optional parameters, you can find a few additional suggestions than we already covered.

Bozho Bozho k gold badges silver badges bronze badges. But, then we have to create one method per parameter unless we group some parameters into an object, eg. Also, henning77 says its not okay from a practical perspective due to all the NPEs it could cause.

I use a very simple rule: Never allow null as an argument or return value on a public method. Great links! As an experienced programmer just gaining more literacy in Java, this is very useful. But Bozho says its okay to allow null arguments sometimes. Would you agree? Optional ist just another kind of NULL. If you're serious about this, you shouldn't use Optional either — Bastian Voigt.

Tim Barrass Tim Barrass 4, 2 2 gold badges 23 23 silver badges 48 48 bronze badges. Thanks, Can u please provide some good links for same if you already know? Null object pattern can be useful for some of scenarios like returning collection not in all cases like in case its single object i will return NULL. The rule is: simple interface, complicated implementation.

Michael Lorton Michael Lorton Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name.



0コメント

  • 1000 / 1000