Solved

check if a field can be set to null

Posted on 2004-10-15
6
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

688 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