Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Cant we give object names dynamically

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create biginteger object dynamically but its throwing me an error.


below is the error which I am getting.




 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't create variable names dynamically. If you want to associate values with run-time names, look into a Map implementation.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects do not have names. Variables have names. A variable is not an object; it's important to understand the difference between variables and objects. A variable (of a non-primitive type) is a reference to an object. You can have multiple variables that refer to the same object.

Also, you have to understand the difference between compile time and runtime.

Compile time is when the compiler translates your source code to an executable class file.

Runtime is when you run the compiled program. Runtime is always after compile time - you first have to compile a program before you can run it.

Many concepts in Java programming, including names of variables, have to be known at compile time. What you are trying to do in the code you posted is create a variable name at runtime. That is not possible, because the variable name has to be known at compile time, which is before runtime.

If you want to create and store objects dynamically, you'll have to store them in a collection such as a List, Map or Set. Have a look at the Collections tutorial from Oracle to learn about collections.
 
lowercase baba
Posts: 13048
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i always figure variable names are for programmers who are reading code.  What possible advantage is there to creating a variable name at runtime, when the programmer can't see it?
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:i always figure variable names are for programmers who are reading code.  What possible advantage is there to creating a variable name at runtime, when the programmer can't see it?



The advantage of dynamically-generated variable names would be exactly the same advantages that Map implementations already have.  You don't need to know the "key" to get a given value at programming/compile time, but it will be known at run time.
 
Ranch Foreman
Posts: 250
5
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:i always figure variable names are for programmers who are reading code.  What possible advantage is there to creating a variable name at runtime, when the programmer can't see it?



Do you know you can create an ArrayList that can hold references to objects dynamically?
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yosuf Ibrahim wrote:
Do you know you can create an ArrayList that can hold references to objects dynamically?



Yes, but the question was about assigning names to it dynamically. An ArrayList doesn't allow a name, or anything (but an index) to be attached dynamically. This is why, Bear recommended using a Map, and not a List, in his above post.

Henry
 
Ryan McGuire
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to the original post...

There are three levels of improvement you could make to the code:
1. Implement Bear's suggestion by using a Map of some kind.



2. You should then notice that the temporary BigInteger values aren't being used outside the for() loop and only one is being used each time through the loop.  Therefore, you don't need to have a different BigInteger variable for each value you read in.  Just reuse the same name each time through the loop:



3. Even better... just assign the new BigInteger to the bigInt array element without using the temporary variable.


 
Have you no shame? Have you no decency? Have you no tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic