---
title: "Create variable dynamically in java and what are data types of variable"  
description: "Create variable dynamically in java and what are data types of variable"  
author: "Allen Scott"  
published: 2014-11-13  
updated: 2014-11-13  
canonical: https://www.mindstick.com/forum/12571/create-variable-dynamically-in-java-and-what-are-data-types-of-variable  
category: "java"  
tags: ["variable"]  
reading_time: 1 minute  

---

# Create variable dynamically in java and what are data types of variable

As we know [variables](https://www.mindstick.com/articles/715/php-variables) are of different [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) types, but which data type are their names of?\
As it seems they are [String](https://www.mindstick.com/articles/1527/string-split-in-c-sharp), but if they are String then this should be allowed:

int i=6;

String [] arr+i;

...as we can [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) an int to a String.\
So if these are not String then what are they?\
And if we want to create [variable names](https://www.mindstick.com/forum/160065/restrictions-imposed-by-strict-mode-on-the-usage-of-reserved-keywords-and-variable-names) dynamically how can we create it? By dynamically I [mean](https://yourviews.mindstick.com/view/80768/what-does-real-minority-mean) whenever the [user clicks](https://answers.mindstick.com/qa/34556/if-a-user-clicks-all-the-google-ads-on-my-site-is-it-foul) on a specific JComponent, a new variable is created, like:

int i=0;

//on first [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social)

String str+i; ///str0

i++;

///on 2nd click

String str+i; ////str1

///i++;

How can I do it?

## Replies

### Reply by Mark M

You can not create dynamic variables in Java because Java isn't a scripting language. YOu need to create variables in source code. But Java provides other ways to do this. You can use arrays or Map<String, String> etc for this purpose.

Map<String, String> map= new HashMap<>();

int i=0;

while(true) {

// you can use whatever condition you need

details.put("key" + i, "val: "+i);

i++

// some condition to break the loop

}


---

Original Source: https://www.mindstick.com/forum/12571/create-variable-dynamically-in-java-and-what-are-data-types-of-variable

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
