• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant we give object names dynamically  RSS feed

 
somashaker goud
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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.




 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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?
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • 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.
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • 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.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!