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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:14:32+00:00 2026-05-26T05:14:32+00:00

In below class I am adding a String to a Vector(v1) and then adding

  • 0

In below class I am adding a String to a Vector(v1) and then adding that Vector(v1) to a new Vector(v2). I then re-initialise Vector v1.

How does java retain the reference to v1 ? When I re-initalise v1 is java maintaining a reference under the hood ?

The output of below is “1”.

public class VectorTest {

    public static void main(String args[]){
        new VectorTest().testVector();
    }

    private void testVector(){

        Vector v1 = new Vector();
        Vector v2 = new Vector();

        v1.add("1");
        v2.add(v1);
        v1 = new Vector();
        Vector v3 = (Vector)v2.get(0);
        System.out.println(v3.elementAt(0));
    }

}
  • 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-05-26T05:14:32+00:00Added an answer on May 26, 2026 at 5:14 am
    private void testVector(){
    
        Vector v1 = new Vector(); // v1 references some vector created on the heap
        Vector v2 = new Vector(); 
    
        v1.add("1"); // vector referenced by v1 gets a "1" added
        v2.add(v1); // make v2[0] the second referrer to that Vector you created
    
        v1 = new Vector(); // Change what v1 refers to, this is not the same as destroying the original Vector
        // v2[0] still holds a reference to the original vector
    
        Vector v3 = (Vector)v2.get(0); // v3 is now another referrer to the same vector
        System.out.println(v3.elementAt(0));
    }
    

    You’re changing what v1 refers to, in this case some other Vector. But v2 still holds a reference to the original Vector.

    Objects in Java can have multiple variables referring to it. Vector objects are also mutable, meaning that if one of its referrers modifies it, like you do with:

    v1.add("1");
    

    Then every other referrer will now reference that same vector that has a "1" in it.

    You can also change what objects a variable refers to, like you do here:

    v1 = new Vector();
    

    So after that happens, v1 holds a different reference to a different Vector object, while v2[0] (excuse the array notation) still holds a reference to the original Vector you created and assigned to v1 at the beginning.


    Vector v3 = (Vector)v2.get(0);
    

    When you do this, you’re making v3 refer to the same Vector that you created at the beginning, so there are two referrers to this object at this point:

    1. v2[0]
    2. v3

    I’d try to draw some crude images to demonstrate this, but I think it’s easier just to read this page from the Java tutorial about objects and references:

    http://download.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

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

Sidebar

Related Questions

Consider the class below that represents a Broker: public class Broker { public string
i need Enumarable value from below class but give error public static Enumerable LoadDataByName(string
I have three model classes that look as below: class Model(models.Model): model = models.CharField(max_length=20,
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I'm getting this error message with the code below: class Money { public: Money(float
I have a data structure which is as given below: class File { public
I have a question, here are two classes below: class Base{ public: virtual void
I'm a bit confused about the object references. Please check the examples below: class
In the class below, I am using a singleThreadScheduledExecutor. My question is, do I
In the class below, only the second constructor takes a ForumThread object. Otherwise, it

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.