Solved

check if a field can be set to null

Posted on 2004-10-15
6
218 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now