Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9178429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:32:57+00:00 2026-06-17T17:32:57+00:00

I have basic class : public class SomeClass { private List<String> list = new

  • 0

I have basic class :

 public class SomeClass {

    private List<String> list = new ArrayList<String>();

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }   

}

In a method I have this code :

private void test(){

    SomeClass sc1 = new SomeClass();
    sc1.getList().add("a");
    sc1.getList().add("b");

    SomeClass sc2 = sc1;

    System.out.println(sc2.getList().size());

    sc1.getList().remove(0);
    System.out.println(sc2.getList().size());

    sc1=null;
    System.out.println(sc2.getList().size());

    sc2=null;
    System.out.println(sc2.getList().size());


}

I receive :

  • 2
  • 1
  • 1
  • NullPointerException

but I though I would received :

  • 2
  • 1
  • NullPointerException
  • NullPointerException (if previous NullPointerException caught)

If a referenced object is set to null, the reference link is broken ? Thank you for clarifications

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:32:59+00:00Added an answer on June 17, 2026 at 5:32 pm

    If a referenced object is set to null, the reference link is broken ?

    There’s no such concept as setting an object to null, You can only set a variable to null. This statement:

    sc1=null;
    

    … changes the value of sc1 and that is all.

    A variable is simple a storage location with a name. The storage location has a value, and for variables whose type is non-primitive, that value is a reference. It’s very important to understand that the value of the variable is not an object. It’s just a reference.

    So this statement:

    SomeClass sc2 = sc1;
    

    just declares a new variable (sc2) which starts off with the same value as an existing variable (sc1). The two variables are completely separate – they just happen to have the same value to start with.

    I like to think of variables as pieces of paper. A piece of paper can have some primitive value on it, such as a number, or it can have a house address1. Imagine these steps:

    • Build a house
    • Write the address of that house on a piece of paper (sc1)
    • Copy the writing on sc1 onto another piece of paper (sc2)
    • Rub out the address on sc1
    • Visit the house whose address is on sc2

    That’s similar to what you’re doing here… and clearly rubbing out the value on sc1 doesn’t affect either the house or the sc2 piece of paper.

    Now as for why you see 2 and then 1… imagine these steps:

    • Build a house
    • Write the address of that house on a piece of paper (sc1)
    • Use sc1 to find the house, and put two parcels on the doorstep
    • Copy the writing on sc1 onto another piece of paper (sc2)
    • Use sc1 to find the house, and shout out how many parcels you find
    • Use sc1 to find the house, and take one parcel away
    • Use sc2 to find the house, and shout out how many parcels you find

    The first time you’ll shout 2, and then you’ll shout 1. You’re not changing the values on the pieces of paper – you’re changing the house itself, by adding or removing parcels.


    1 I know that references aren’t memory addresses necessarily. This is purely for the sake of the analogy, which is one I’ve found helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic question. I have this ArrayList: @Named(AccountProfileController) @ViewScoped public class AccountProfile
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I have a basic class 'HistoryItem' like so: public class HistoryItem private Date startDate;
Suppose I have very basic Task class as follows: public class Task { string
I have the following basic entities: public class Basket { public List<Product> Products {get;set;}
I have this basic JFlex lexer : import java.util.*; %% %public %class TuringLexer %type
I have this basic setup: enum{ BASE, PRIMITIVE, ... }; class IUnknown{ public: bool
Say I have a couple of basic objects like so: [Serializable] public class Base
I have very basic class: class Customer { protected $id; protected $customer; public function
I have a class that contains a List<> of objects. For this example I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.