Skip to content

Tag: variables

Duplicating objects in Java

I learned that when you modify a variable in Java it doesn’t change the variable it was based on I assumed a similar thing for objects. Consider this class. After I tried this code I got confused. Please explain to me why changing any of the objects affects the other one. I understand that the value of variable text is

Is it possible to create variables at runtime in Java?

For example, say I wanted to “extract” String[] fruits = {“Pear”, “Banana”, “Apple”}; into three separate variables, eg: How could I do that, ignoring the “Why the heck would you want to do that?” question that you might be urged to ask me. Similar questions have been asked many times before, but the real answer was never given, because what

What is the use of a private static variable in Java?

If a variable is declared as public static varName;, then I can access it from anywhere as ClassName.varName. I am also aware that static members are shared by all instances of a class and are not reallocated in each instance. Is declaring a variable as private static varName; any different from declaring a variable private varName;? In both cases it