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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:40:13+00:00 2026-06-06T19:40:13+00:00

public static MyType mtOrders; public static MyType mtCustomers; public static MyType mtItems; public static

  • 0
public static MyType mtOrders;    
public static MyType mtCustomers;    
public static MyType mtItems;    
public static MyType mtGroups;    
public static MyType mtDelieverAddresses;    
public static MyType mtVendors;    
public static MyType mtOrderItems;    
public static MyType mtPrims;        

public final static MyType[] xTable = {mtCustomers, mtGroups, mtItems, mtOrders,
                        mtDelieverAddresses, mtVendors, mtOrderItems, mtPrims};

for (int i = 0; i < xTables.length; i++) {    
  xTable[i] = new MyType();
}

After executing xTable‘s elements are initialized, but mtOrders… mtPrims are null!

I understand why this is so, but I can not think how I initialize the objects in the loop.

I do not want to do this:

mtOrders = new MyType();
mtCustomers = new MyType();
...
mtPrims = new MyType();
  • 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-06T19:40:15+00:00Added an answer on June 6, 2026 at 7:40 pm

    xTable and that series of static variables are different references, that might point on the same object, but cannot be used to modify each other. Namely, they can both be used to modify the same object, but having one point on an object will not make the other point on that object as well.

    I don’t quite understand your actual goal. Best I can offer is to use some intermediate class they will all point to. So mtOrders will not be a MyType, but a MyTypeRef, which will have a MyType field. Then, do initialize it to a new MyTypeRef, and use the loop to set the MyType objects in all the refs. Since both the static variables and the xTable entries will point on the same MyTypeRef, setting the MyType field on one will affect the other as well.

    IMO, using that loop instead of initializing each separate variable looks like tighter code, but it doesn’t work. Just write a few more lines of code, so it’s not as pretty but actually works.

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

Sidebar

Related Questions

public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
public static int AwaitingApprovals() { int numApprovals = 0; string sql = SELECT COUNT(Type)
Ok, this is what I want to do: public static void CallStaticMethod(Type myType, String
Where can I find conversion methods such as public static implicit operator MyType(OtherType d)
public static void main(String[] args) { int count, innerCount; for(count=0;count<=3;count++) { System.out.println(Count is +
public static void main(String args[]) { int[] intarray = {1, 3, 6, 8, 2,
public static class stClass { static Class1 obj = new Class1(); public static int
public static Connection connect; public static ResultSet resultSet; static final String DATABASE_URL = jdbc:mysql://localhost/java_dev;
public static class TestTask extends AsyncTask<Void, Integer, Integer> { private String stacktrace; public TestTask

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.