?
Solved

check if a field can be set to null

Posted on 2004-10-15
6
Medium Priority
?
224 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 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 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 Month8 days, 6 hours left to enroll

764 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