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

  • SEARCH
  • Home
  • 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 7715975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:31:18+00:00 2026-06-01T02:31:18+00:00

Some question on java basics, the below function returns an object of Node type

  • 0

Some question on java basics,
the below function returns an object of Node type

class DS{
    public Node getNode(int index){
        return nodeList.get(index);
    }
}

public void test1(){
  DS ds = new DS();
  Node node = ds.getNode(3);
  // will the change in node variable(of test1()) change the actual Node object in ds?
  // Is there a simple way to create a copy to prevent source node's data modification?
}

Thanks in advance.

  • 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-01T02:31:20+00:00Added an answer on June 1, 2026 at 2:31 am

    The getNode() method on the DS class returns a reference to the node stored in nodeList. As such, if your Node class can be mutated (via setter methods or direct access to its fields), then yes, your code in test1 will change the underlying (and now shared) Node reference.

    If you want to “disconnect” the returned node from the node in your datastore, then you can clone it first. Your node class will need to implement Cloneable and override the clone() method. If you have only primitives in Node, then you can simply do:

    public Object clone()
    {
      return super.clone();
    }
    

    If you have other objects in Node, then the default clone operation will only make a shallow copy, and you will need to make your clone method more extensive so that it makes deep copies. From the Javadoc for Object.clone():

    By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal “deep structure” of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

    The change to your DS class will be:

    public Node getNode(int index){
        return (Node) nodeList.get(index).clone();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got some questions about Java's assigment. Strings I've got a class: public class
Really basic question I'm sure for some of you Java heads out there. I
So, i have some question about xml Documents in Java. Can i get all
I asked a question some time ago on java 2d pathfinding... Pathfinding 2D Java
I have Java experience and recently am doing some C++ coding. My question is
This is a real beginner question (I'm still learning the Java basics). I can
This is a Java syntax question, but just for some background. Using android, I
I've got some questions regarding Java design patterns. I want to know what the
I'm trying to learn linked-lists in Java and had some questions about the code
We are using TFS 2010 with gated checkins. I've got some question about what

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.