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 7865807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:11:55+00:00 2026-06-03T00:11:55+00:00

In my original code, I’m adding nodes to a tree. My goal is to

  • 0

In my original code, I’m adding nodes to a tree. My goal is to somehow get access to the last node that I added in the tree (my idea was to create another object that will point to the last object (node in my original example)).

public class RandomSubClass 
{
    int firstNum;
}    

import java.util.LinkedList;

public class RandomClass 
{
    LinkedList<RandomSubClass> myListOfObjects = new LinkedList<RandomSubClass>();

    void addItem(int firstNum, RandomSubClass toBeReturned)
    {       
        RandomSubClass o1 = new RandomSubClass();
        o1.firstNum = firstNum;
        myListOfObjects.add(o1);

        // Here I was thinking that 'toBeReturned' will get same address as
        // 'o1', and by changing 'toBeReturned' (in main), values in 'o1' change
        //
        toBeReturned = o1;

        // This following commented code worked, 
        // but I can't use it in my original code.
        //
        // The reason I can't use it is because when I add a node to a tree, 
        // I start at the root and trace the new node's way to a new leaf, 
        // which makes it hard to do (simply) that.
        //
        //toBeReturned.firstNum = firstNum;
        //myListOfObjects.add(toBeReturned);
    }   
}

public class Main 
{
    public static void main(String[] args) 
    {
        RandomClass myList = new RandomClass();

        RandomSubClass r1 = new RandomSubClass();
        RandomSubClass r2 = new RandomSubClass();

        myList.addItem(1, r1);
        myList.addItem(2, r2);

        // I would like to do that, and see changes in the node in 'myList'
        //  
        r1.firstNum = 10;
        r2.firstNum = 20;
    }
}

I want to check something about the node after I add it to the tree, and if it satisfies some condition, I want to change a flag for that node.

I can re-trace the node again (starting at root), but my tree might get huge at some point and it will take time. So if I get the address of that node when I add it, and after I check my condition, I can modify the flag at that address, knowing that it will change the flag at that node (last added).

  • 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-03T00:11:56+00:00Added an answer on June 3, 2026 at 12:11 am

    Yes, you can do this. I give you permission. 🙂 But your example code won’t work because Java objects are passed by value, not by reference. That is, when you pass an object to a function, if you reassign that object, it has no effect on the caller. For example:

    void caller()
    {
      String s1="Hello";
      updateString(s1);
      System.out.println(s1);
    }
    void updateString(String s1)
    {
      s1="Goodbye";
    }
    

    The output of this function is “Hello”, NOT “Goodbye”. The assignment within the updateString function does not change the value passed in by the caller.

    There are (at least) three ways to do what you want.

    Method 1: The simplest is to return the new object, rather than updating a parameter:

    SomeObject addItem(int firstnum)
    {
      SomeObject o1=new SomeObject();
      o1.firstnum=firstnum;
      objectList.add(o1);
      return o1;
    }
    ...
    void callingFunction()
    {
      SomeObject newObject=addItem(1);
      newObject.secondnum=2;
      ... etc ...
    }
    

    I prefer this method what I don’t need to have some other return value: it’s clean and simple.

    Method 2: Create a global variable and store a handle of the object there. But this method sucks, because globals suck in general. I only mention it to tell you not to do it.

    Method 3: Create a wrapper to hold a reference to the object. Then pass the wrapper to the “add” function, which can update the value within the class.

    class SomeObjectWrapper
    {
      public SomeObject someObject;
    }
    ...
    void addItem(int firstnum, SomeObjectWrapper sow)
    {
      SomeObject o1=new SomeObject();
      o1.firstnum=firstnum;
      objectList.add(o1);
      sow.someobject=o1;
    }
    ...
    void callingFunction()
    {
      SomeObjectWrapper sow=new SomeObjectWrapper();
      SomeObject newObject=addItem(1, sow);
      sow.someObject.secondnum=2;
      ... whatever ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: original question below, but I revise it now that I have some code
###########Update Here is the original code that worked in ie7 but not in 9.
The original code looks like this,it works. def my_index(request): global account if request.session.get('account',False): account=request.session['account']
I have some original code that manages exception safety like this: void foo() {
The original code: public List<Contact> GetContactListEntityCompiledLINQ() { if (entities == null) entities = new
here is the original code: public static int getInt () { Scanner in =
I have a plugin where the original code is ... // when the DOM
I need to migrate some C# code to Python. The original code makes use
The code below is (hopefully) a minimized testcase of my original problem. I'm trying
from this code will show in original color how can i change it to

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.