• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

check if a field can be set to null

How do you check if a field can be set to null?  Can you write code that throws an exception......

2 Solutions
Tommy BraasCommented:
Hi bcsmess,

All non-native field types in Java can be set to null. If you have accessors to all variables you can enforce that only non-null values are set.


orangehead911, how about:

class MyClass {
    private static final String myString = "hello";

    private void setNull() {
        // This is not allowed....
        myString = null;

Of course, my example is a compile error... but yes, all non-final attributes can be set to null...
bcsmessAuthor Commented:
how about final attributes?
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

You will get a compilation error if you try to set a value to a final field.
as I pointed out...
Tommy BraasCommented:
> orangehead911, how about:

Stating the obvious, final fields aren't variables...they are constants.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now