Solved

check if a field can be set to null

Posted on 2004-10-15
6
222 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 175 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 175 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
ejb example issues 3 53
wild fly 8 startup error 2 42
Java pass by reference 3 21
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

733 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