No. They won't be equal to anything because they won't exist. Nothing ever created an instance of Class so there is no instance.

As an analogy you're basically asking, "If I don't build a house, will that house's windows be open or closed?" They will be neither.

When you do this:

Class object = null;

What you have is a variable which can (at a later time) refer to any instance of type Class, but which currently refers to no instance.

is there a way for me to create the object so that all three instance variables are null

Kind of. You can add a constructor which doesn't set those values:

public Class () { }

And you can create an instance of that class via that constructor:

Class object = new Class();

Then you can observe your instance in the debugger to see what those values are. I'm not 100% sure in Java, but it's possible that an int can't be null. Which would make that part of the question kind of moot. (I know it can't in C#, but if it can in Java then ignore this part and carry on.)

An unassigned int local variable would be a compiler error if you try to use it. But this is a class field, not a local variable. In this case it's going to be automatically given its default value, which is generally null for reference types but 0 for primitive numeric types.

Answer from David on Stack Overflow
🌐
DZone
dzone.com › coding › languages › null object pattern in java
Null Object Pattern in Java
August 22, 2018 - In this tutorial, we demonstrate how to use the null object pattern in Java, which depends on the Factory class and Optional class to represent non-null values.
🌐
Admfactory
admfactory.com › home › how to create null object pattern in java
How to create Null Object Pattern in Java | ADMFactory
December 7, 2016 - The implementation for the real object. package com.admfactory.pattern; public class User implements Model { private int id; private String name; public User(int id, String name) { this.id = id; this.name = name; } @Override public int getId() { return id; } @Override public String getName() { return name; } @Override public String toString() { return String.format("[id:%d,name:%s]", getId(), getName()); } } The implementation for the Null Object.
🌐
Refactoring.Guru
refactoring.guru › home › techniques › simplifying conditional expressions
Introduce Null Object
January 1, 2026 - } // Replace null values with ... is creating yet another new class. From the class in question, create a subclass that will perform the role of null object....
🌐
SourceMaking
sourcemaking.com › design_patterns › null_object › java › 1
Null Object Design Pattern in Java
The advantage of this approach over a working default implementation is that a Null Object is very predictable and has no side effects: it does nothing. For example, a function may retrieve a list of files in a folder and perform some action on each. In the case of an empty folder, one response may be to throw an exception or return a null reference rather than a list.
Top answer
1 of 2
2

No. They won't be equal to anything because they won't exist. Nothing ever created an instance of Class so there is no instance.

As an analogy you're basically asking, "If I don't build a house, will that house's windows be open or closed?" They will be neither.

When you do this:

Class object = null;

What you have is a variable which can (at a later time) refer to any instance of type Class, but which currently refers to no instance.

is there a way for me to create the object so that all three instance variables are null

Kind of. You can add a constructor which doesn't set those values:

public Class () { }

And you can create an instance of that class via that constructor:

Class object = new Class();

Then you can observe your instance in the debugger to see what those values are. I'm not 100% sure in Java, but it's possible that an int can't be null. Which would make that part of the question kind of moot. (I know it can't in C#, but if it can in Java then ignore this part and carry on.)

An unassigned int local variable would be a compiler error if you try to use it. But this is a class field, not a local variable. In this case it's going to be automatically given its default value, which is generally null for reference types but 0 for primitive numeric types.

2 of 2
1

Let's say I have a class named Class

Given that java.lang.Class already exists, let's not. Let's say you have a class named MyClass.

and I created a new null object:

That's an oxymoron.

null is a reference. It's not an object. It is, in fact, the reference that means 'I refer to no object', and is the only reference that means 'I point at nothing'.

When you write:

MyClass x = new MyClass();

Then x is a treasure map, and new MyClass() is 'create a new treasure chest and bury it in the sand'. The = in the middle says: Update the treasure map named x, so that following it would lead you to the treasure I just made (new X() means: Make new treasure and bury it).

MyClass x = null;

means you have a treasure map named x which is currently blank.

If not, is there a way for me to create the object so that all three instance variables are null?

That would imply a treasure chest of the MyClass treasure type, which has room for some scratches (int a - a number), and which contains a treasure map (the String b variable). If you want to set them all to null, well, you can't - a is a primitive (int) and those aren't treasure maps, they are the actual number. You can't not have one - a cannot be null. At best, a can be 0. b CAN be null. That means there's real treasure, but the treasure contains yet another treasure map (in java it's mostly treasure maps all the way), but that one is blank. That's different from there being no treasure at all.

More generally, the question: "Can I make a new instance of MyClass such that all fields are some value I desire" is the wrong question to ask, perhaps: The general principle is encapsulation: MyClass is an API: It's like the receptionist at a big corp's office. The receptionist decides what services are available. Even if the CEO is available, if the receptionist elects not to answer the question 'can I see the CEO right now please?', then you can't see her.

Your question boils down to: "If I storm into this office and I demand to speak to the CEO, will I be allowed to?" - the only viable answer is: Well, the receptionist would decide, so you'd have to ask him. For classes: Whatever the API lets you do, you can do. But that's all you can do.

If there is no constructor that initializes these fields to null, then, no, you can't.

🌐
Coderanch
coderanch.com › t › 386100 › java › Setting-objects-null
Setting objects to null (Java in General forum at Coderanch)
August 28, 2008 - If the reference is local to a method then setting it to null just before the method returns will make no difference because as soon as the method returns, the reference goes out of scope and so is not considered to refer to the object anymore anyway. If the reference is an instance variable, then setting it to null makes the object available for GC as long as there are no other references to it.
🌐
Coderanch
coderanch.com › t › 647047 › java › create-object-null-class-constructor
How to create an object to be null if the class's constructor parameter is int? (Beginning Java forum at Coderanch)
The standard approach is to check before you use an object: ... Oh sorry for the confusion. I have to make a few constructors under Author class one is something like another one is And now I get what you mean with but the second constructor which I still don't get how I can fix it so null can be entered. ... if( year == 0 && month == 0 && day == 0 ) died = null; Edit: I would be more inclined to do away with the three argument constructor entirely. JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › design_pattern › null_object_pattern.htm
Design Patterns - Null Object Pattern
Such Null object can also be used to provide default behaviour in case data is not available. In Null Object pattern, we create an abstract class specifying various operations to be done, concrete classes extending this class and a null object class providing do nothing implemention of this class and will be used seemlessly where we need to check null value.
🌐
Wikipedia
en.wikipedia.org › wiki › Null_object_pattern
Null object pattern - Wikipedia
October 20, 2025 - This code illustrates a variation of the C++ example, above, using the Java language. As with C++, a null class can be instantiated in situations where a reference to an Animal object is required, but there is no appropriate object available. A null Animal object is possible (Animal myAnimal = null;) and could be useful as a place-holder, but may not be used for calling a method.
🌐
Java Design Patterns
java-design-patterns.com › patterns › null-object
Null Object Pattern in Java: Streamlining Error Handling with Graceful Defaults | Java Design Patterns
Learn how the Null Object Pattern simplifies your Java code by handling null references effectively. Discover its implementation, advantages, and practical use cases.
Top answer
1 of 7
59

Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)

Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:

ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;

... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:

list1 = null;
System.out.println(list2.size()); // Just prints 0

Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:

list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo

So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:

List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;

... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.

2 of 7
10

To clear the variable

According to my knowledge,

If you are going to reuse the variable, then use

               Object.clear();

If you are not going to reuse, then define

               Object=null;

Note: Compare to removeAll(), clear() is faster.

Please correct me, If I am wrong....

🌐
Stack Overflow
stackoverflow.com › questions › 50532791 › java-pass-null-reference-to-a-constructors-object-and-create-the-instance-lat
Java - Pass null reference to a constructor's object and create the instance later - Stack Overflow
It's not clear from your question what does the Th(th[(i+1)%(N)],first,shared,i,N,counter), but since it throws a NullPointerException when passed a null as the first argument, you cannot use it at least for one of the Th instances. I suggest you create the first Th instance with the Th(Th n) constructor, passing null to it:
🌐
SourceMaking
sourcemaking.com › design_patterns › null_object
Null Object Design Pattern
Both of these can probably be implemented with essentially no code. All the methods in these classes do "nothing". They are perfect examples of the Null Object Pattern. The key to the Null Object pattern is an abstract class that defines the interface for all objects of this type.
🌐
Howtotrainyourjava
howtotrainyourjava.com › tag › null-object
Null object – How To Train Your Java
Usually cleaner and safer than null reference. Third idea, if your object contains a collection, initialize it. Don’t rely on it being delivered via setter later. It is likely that the setter won’t be called, “because of reasons”. But if you put an empty collection at object creation, you are safe to iterate over it later, unless somebody explicitly sets null there, which is rather unlikely (you may also hide the setter itself if you like).
Top answer
1 of 2
3

A solution such as Option<T> is not a so bad idea. In fact, it has been successfully introduced in Haskell as a Maybe (but I don't know if this is the first time this solution has been used). Maybe monads are common in Haskell programs. Since, other languages have adopted the same system. For instance, Scala has an Option type.

If you don't want to become dependent of Guava, you could use Java 8, which introduces the Optional class.

However, Option/Maybe/… types are more relevant in a functional environment, because what you typically want to do is to get a behaviour/a property from the element if it really exists, and to get nothing or a default behaviour/property otherwise. So, a typical use of Options consists in

  • applying a function A->B on the Option[A] in order to get an Option[B];
  • switching your code on the basis of the real content of the Option (for instance, by using Pattern matching if the language supports it and if Option has two subtypes, or by using method overload);
  • or filtering the Options that represent (non-)existing elements.

As an alternative, you could use the Null Object Pattern, in which you have an abstract class MyClass and two concrete subclasses: MyRealClass and MyNullClass. You manipulate instances of MyClass, but generate instances of MyRealClass (if the element is really existing) or MyNullClass (if the element doesn't exist). MyNullClass contains the default behaviours/properties. If the null objects are stateless (which is typically the case), one could cache them, or even make them singletons.

This pattern is described in [Fowler].

[Fowler] Martin Fowler, Kent Beck, Refactoring: Improving the Design of Existing Code.

2 of 2
2

The whole point of a value object is that equality isn't based on identity; you could very well have two distinct blank objects. In fact, whether all blank objects are the same or different objects should be an implementation detail. For those reasons, the second approach is not good. If you look at the Java 8 APIs, there's a notion of a "value-based class" with the following properties:

  • are final and immutable (though may contain references to mutable objects);
  • have implementations of equals, hashCode, and toString which are computed solely from the instance's state and not from its identity or the state of any other object or variable;
  • make no use of identity-sensitive operations such as reference equality (==) between instances, identity hash code of instances, or synchronization on an instances's intrinsic lock;
  • are considered equal solely based on equals(), not based on reference equality (==);
  • do not have accessible constructors, but are instead instantiated through factory methods which make no committment as to the identity of returned instances;
  • are freely substitutable when equal, meaning that interchanging any two instances x and y that are equal according to equals() in any computation or method invocation should produce no visible change in behavior.

The documentation further adds:

A program may produce unpredictable results if it attempts to distinguish two references to equal values of a value-based class, whether directly via reference equality or indirectly via an appeal to synchronization, identity hashing, serialization, or any other identity-sensitive mechanism. Use of such identity-sensitive operations on instances of value-based classes may have unpredictable effects and should be avoided.

There's no way to disable the == operator in Java so that warning is the best you can do.

Thus, your first approach is correct, with the caveat that foo1.equals(foo2) should always be true when foo1.isBlank() and foo2.isBlank() even if foo1 != foo2.

Top answer
1 of 7
16

No, because a is a reference (not an object as in this question's title) and no method can modify the value of a reference except the method in which it is defined (I assume from the code context that a is a local variable).

Since Java doesn't have pass-by-reference, what you ask cannot be done: there's no way to collect addresses-of references in order to manage the addresses pointed to. You might use a wrapper object, but not sure what'd be the point.

2 of 7
5

As everyone else has said, this simply isn't possible. If it's cleaning up resources you're after, then you might consider using a pattern such as:

class A {

    private boolean cleanedUp;

    public void cleanUp() {
        // clean up any resources
        cleanedUp = true;
    }

    public boolean isCleanedUp() {
        return cleanedUp;
    }
}

And then using it like so:

A a = new A();
a.cleanUp();
if (a.isCleanedUp()) {
    ...
}

A better solution might be to implement the java.io.Closeable or java.lang.AutoCloseable interfaces depending on your circumstance:

class B implements AutoCloseable {

    private boolean closed;

    public boolean isClosed() {
        return closed;
    }

    @Override public void close() throws Exception {
        // clean up any resources
        closed = true;
    }
}

In which case you can use a try-with-resources statement:

try (B b = new B()) {
    // do stuff
} catch (Exception ex) {
    // oh crap...
}

Or you could even combine the two and do it that way, whichever you prefer.

Or lastly you could do it the way William Morrison explained (though I'd probably cheat and just use java.util.concurrent.atomic.AtomicReference instead of making my own class, and it comes with the added benefit of being a generified type), which, depending on your circumstance, may really be unnecessary. After all, you could always just do (even though it might seem a little odd):

A a = new A();

a.doStuffAndDisappear();
a = null;

if(a == null){
     //...
}
🌐
GeeksforGeeks
geeksforgeeks.org › system design › null-object-design-pattern
Null Object Design Pattern - GeeksforGeeks
March 8, 2024 - The Null object pattern is a design pattern that simplifies the use of dependencies that can be undefined. This is achieved by using instances of a concrete class that implements a known interface, instead of null references.