• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

How to create many objects and how to use variables as object names?

 
Greenhorn
Posts: 17
1
IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a question I can't get my head around, thank you for helping.

I understand how to make objects but how would you create 2000 objects from a class and you read the object names from a file and you added them to a list that you can iterate to create all the objects with the names of that StringList?

I can't loop it, and do this


how do you do this? For example, I have 2000 items in a StringList and I want to create 2000 objects that have as name these 2000 String names that are in that list.

Or do you create 1 class object containing a list of these 2000 names? I'm clearly missing something here.

Each object contains certain values, that's why I would like to have 2000 objects that contains this data so i can iterate over that list and do actions on them? Is my thinking correct?

Thank you for helping.
 
Marshal
Posts: 76887
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't say “object names” because objects don't have names. Only say “class objects” if you are creating objects to represent classes, as you would with the Class class. Variables have names, but it is usually very difficult to read variable names from a file. You write the variable names in the code, and they would be very difficult to change by reading a file. So that suggestion bites the dust.
Yes, you can read from your file; I might do something like this:-If you don't understand line 7, please ask.

Now, you said you would create 2000 instances using the lines of the file. Let us therefore give your class (Foo) a one‑argument constructor:-So you can pass a line to that constructor and get a Foo object. That is highly dependent on the lines on the file having the correct format and the constructor taking that particular format, but let's imagine for the moment that this file and this constructor do in fact match like that
Now, you can create a List:-I have copied much of that code unchanged from earlier; only lines 1 and 9 are different. I would probably use a Stream; there are several ways to get a Stream and what follows is only one way to use it. Sorry I haven't got the time to explain more fully just now:-In both cases you will get a List with size the same as there are lines in your file. You could populate an array, but you need to be sure that the length of the array is the same as the line count. I can't offhand think of a really different way to create 2000 objects like that. It may be possible to use a Scanner to read the file; you can then separate each line into its constituent parts, but that is only a minor change from what I have shown you.
 
Rancher
Posts: 144
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the following has to be said and explained:
You cannot use the content of an Object as the identifier for another reference.
Your post sounds a bit like this: You have a file contain a list of "names" you like to use to access Objects - that is just not possible in Java. If your file contains entries like "a", "b", "c", ... you can not create references at runtime with these: Object a, Object b, Object c, ...
I won't touch reflections here wich would enable you access to existing references, but creating new ones isn't possible even with reflections.
When you want to associate an object with an identifier you can use a Map:

Then you can access the instance with myMap.get(identifier).
That's some basics of the Java language itself you just have to learn and to accept they way they are. There's no other way around (like in PHP where you can use a variable as an identifier for another).
 
B Vancoullie
Greenhorn
Posts: 17
1
IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hansen wrote:you can not create references at runtime with these: Object a, Object b, Object c,



Aha!! I had no idea this wasn't possible.

Let me learn about the maps, thank you both for your help. I had a feeling I was trying to do something wrong.
 
Kristina Hansen
Rancher
Posts: 144
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't say you tried something wrong. This is in fact a very common question from beginners. As them you just didn't knew Java can't do such thing (although as mentioned other languages like PHP do offer such constructs).
There're a few rules you have to accept. Other common questions are like "Why is X as it is and not like Y?" - most often the answer is just: "Because some dev decided so." without any technical reason for it, just a personal opinion.
An example: It took until Java5 for a Base64 class to be made available in the standard public package java.util although some similar already existed before as an internal class for internal use. So every developer had to made the choice of either rely on an internal class not guaranteed to work in other versions or VMs than the one used to compile or use an external lib one had to carry around just for a Base64 converter. Why the devs at Sun didn't made that move earlier noone really knows as Base64 was already a common encoding scheme when Java was first released. It was just a personal choice by someone for what ever reason.
I can't remember if I ever tried to do the same back in my early years of Java, but I guess I sure did as I moved over from PHP back then where this is possible - but even reflecting myself today I won't be able to come up with a use case where it would make sense for me to control the name of a refernce by change a config file instead of just using a collection - as it's the same reference no matter what identifier I chose for it.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic