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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:15:33+00:00 2026-05-18T00:15:33+00:00

using loop, I can create My cat is: Cat1 … My cat is: Cat1

  • 0

using loop, I can create

My cat is: Cat1
...
My cat is: Cat1

However, when I tried to use

  Cat ("cat"+i) = new Cat("Cat" + i);

I’m making mistakes….

So, what is the simplist way to correct my code to produce

cat1 ... cat10 cat instances?


public class TestCat{ 
  public static void main(String [] args){ 

 for (int i=1; i<10; i++){     
   //Cat ("cat"+i) = new Cat("Cat 1");
   Cat cat1 = new Cat("Cat 1");  
   System.out.println("My cat is:  " + cat1 ); 
  }
 } 
} 

class Cat{

 static String catName;
 public Cat(String catName){
   this.catName=catName;
 }
 public String toString(){
   return catName;
}
}

Sorry….I should say

How to create ten 10 Cat instances…..cat1, …cat2…..because in other languages, I can use “cat”||i = …, to create different varaibles, I wonder how I could do similar things in Java….

In other words, I want to name the instances I’m going to create by taking the loop information into account.

  • 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-18T00:15:34+00:00Added an answer on May 18, 2026 at 12:15 am

    Use a collection if you don’t know how many cats you’ll have.

    public class TestCat
    { 
      public static void main(String [] args)
      { 
    
        Cat[] cats = new Cat[10];    
        Vector catsUnlimited = new Vector(10);    
        Cat myCat = null;
    
        for (int i=1; i<11; i++)
        {     
        myCat = new Cat("Cat" + i); 
          cats[i-1]= myCat; 
    
          catsUnlimited.addElement(new Cat("Cat" + i));
    
          System.out.println("My cat is:  " + cats[i-1] ); 
        }
    
        System.out.println("Known cats");
        for (int x = 0; x < catsUnlimited.size(); x++)
        {        
         System.out.println("Cat #" + (x+1) + ":" +(Cat)catsUnlimited.get(x));
        }    
      } 
    } 
    

    Note: This code produces incorrect results and had me stumped until I looked closer at the Cat class.

    My cat is:  Cat1
    My cat is:  Cat2
    My cat is:  Cat3
    My cat is:  Cat4
    My cat is:  Cat5
    My cat is:  Cat6
    My cat is:  Cat7
    My cat is:  Cat8
    My cat is:  Cat9
    My cat is:  Cat10
    Known cats
    Cat #1:Cat10
    Cat #2:Cat10
    Cat #3:Cat10
    Cat #4:Cat10
    Cat #5:Cat10
    Cat #6:Cat10
    Cat #7:Cat10
    Cat #8:Cat10
    Cat #9:Cat10
    Cat #10:Cat10
    

    Here was the culprit:

    public class Cat
    {
         static String catName;
    

    Remove the static and you are golden.

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

Sidebar

Related Questions

I need to build a new list with a loop. Basically i can't use
basically I have a for loop that give me two values, id, and value
I'm trying to create a page which looks somewhat like this: http://www.drmichaelbogdan.com/plastic-surgery/ (I'm referring
I've written a simple graphing implementation in C#, and I can graph things by
I've got a legacy c++ win32 application, and extending it using wpf windows does
I need to perform an exponential operation of two parameters (one set: t, and
I am developing a system that has folders, groups and permissions. Permissions determine what
I have a command bean ( FooList ) which has a property which is
I need to copy a table from one database to another. This will be

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.