?
Solved

check if a field can be set to null

Posted on 2004-10-15
6
Medium Priority
?
228 Views
Last Modified: 2010-03-31

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

0
Comment
Question by:bcsmess
6 Comments
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 525 total points
ID: 12320549
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.

Cheers!

\tt
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 525 total points
ID: 12320647
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...
0
 

Author Comment

by:bcsmess
ID: 12320884
how about final attributes?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Venci75
ID: 12321059
You will get a compilation error if you try to set a value to a final field.
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12321259
as I pointed out...
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 12321638
> orangehead911, how about:

Stating the obvious, final fields aren't variables...they are constants.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question