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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:06:41+00:00 2026-06-11T21:06:41+00:00

How do I instantiate a variable via another variable which is referencing it? For

  • 0

How do I instantiate a variable via another variable which is referencing it? For example:

List<String> list1 = null;
List<String> list2 = list1;

How do I instantiate list1 using list2? I know that list1 can easily be instantiated with list1 = new ArrayList();. But what I want to know is if it is possible to instantiate list1 using list2 given the case above?

Just for clarification: What I want to achieve is to have an access to list1. I have a class which contains list1 and I need to modify the value of list1. unfortunately, that class did not provide a setter for list1 and list1 is still null.

public class Class1
{
    private List<String> list1 = null;
    public List getList1()
    {
        return list1; //value of list1 is null.
    }
}

public class Class2
{
    public static void main(String[] args)
    {
        Class1 class1 = new Class1();

        // then I need to set the value of list1.
        // However, list1 did not provide a setter method
        // so my only way to access it is using a reference.

        // with the code below I am assuming that I can
        // create a reference to list1 and set its value.
        // How do I set the value of list1?
        List<String> list2 = class1.getList1(); 
    }
}
  • 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-11T21:06:42+00:00Added an answer on June 11, 2026 at 9:06 pm

    I think that what you’re asking is if there is some indirect way of initializing list1 by using list2. In Java, the only way to change the value of list1 is to assign something to it.

    In Java, list2 will be null after your code executes. Unlike C++ (or C), Java does not have references to references (or pointers to pointers), there is no way that list2 can be used to indirectly initialize list1.

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

Sidebar

Related Questions

I need to instantiate generic variable like Class>. For example, Class<? extends List<String>> =
Is it possible to dynamically instantiate a class using a variable? For example is
Is it possible to instantiate a class from a string, without declaring another variable
I have the class Variable[X <: SeqVal[_]](initialState:Calc[X]) which I instantiate with new Variable[SeqVal[Float]](Max()) where
I have a class, trying to instantiate another class, based off of a variable
I have a data type and I can instantiate a variable of that type.
How can I instantiate a class by throwing in a variable name? Consider this
Why does my Instantiate function not create a 'Blank' instance of That? I have
i'm attempting to instantiate a bunch of sounds by creating a string array containing
So I have a class where I instantiate a variable callback like so: public

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.