How do I give a predetermined name to an object? [duplicate] - java

This question already has answers here:
Assigning variables with dynamic names in Java
(7 answers)
Closed 8 years ago.
I am trying to create a method that contains a for loop that creates new objects, but I am stuck at how to assign a predetermined name to an object based on the loop's count. For instance it would be something like this:
private void createPictureObject(int count){
for(int a = 1; a <= count; a++){
Picture picture*a* = new Picture(arguments);
}
The result of this loop would be that I have objects named something like picture1, picture2, picture3, picture 4 etc. Is this even possible?

No You cannot do that! Dynamic variable naming is not allowed in Java. Use an array instead.
Picture[] pictures = new Picture[10];

Related

Method not storing data in object myRecipeBox [duplicate]

This question already has answers here:
What is variable shadowing used for in a Java class?
(5 answers)
Closed 8 years ago.
I was reading a book and came across the term Shadow Variables in Java but there was no description for it. Eventually what are these variables used for and how are they implemented?
Instead of providing my own description i may ask you to read about it for example here: http://en.wikipedia.org/wiki/Variable_shadowing. Once you understood the shadowing of variables i recommend you proceed reading about overlaying/ shadowed methods and visibility in general to get a full understanding of such terms.
Actually since the question was asked in Terms of Java here is a mini-example:
public class Shadow {
private int myIntVar = 0;
public void shadowTheVar(){
// since it has the same name as above object instance field, it shadows above
// field inside this method
int myIntVar = 5;
// If we simply refer to 'myIntVar' the one of this method is found
// (shadowing a seond one with the same name)
System.out.println(myIntVar);
// If we want to refer to the shadowed myIntVar from this class we need to
// refer to it like this:
System.out.println(this.myIntVar);
}
public static void main(String[] args){
new Shadow().shadowTheVar();
}
}

What is the formulation for Java Array class names? [duplicate]

This question already has answers here:
What is this: [Ljava.lang.Object;?
(2 answers)
Closed 2 years ago.
I have a need to create the class name of an array in Java. I notice when using reflection to look at class names an array has a certain pattern:
For example for an array of array of com.foo.Thing
[[Lcom.foo.Thing;
What is the formula used to create this class name? I presume other letters besides 'L' mean different things. Are there libraries that help with this?
One letter for each primitive, one symbol for arrays, and one letter for reference:
I = int
J = long
D = double
F = float
Z = boolean
C = char
B = byte
S = short
Lcom/foo/Thing; = reference 'com.foo.Thing'
[ = array of whatever is next (so, [[I is an int[][]).
This is VM-speak for type signatures. For example, the signature of:
public boolean[] foo(String[] args, int count) { ... }
is: ([Ljava/lang/String;I)[Z.
It is for machines and not humans; it is easy to parse (you just move forward, no need to look-ahead).
This is not the 'class name' of a thing; the usual name for this construct is 'vm name'. Note that generics just disappear from these; the VM name of List<String> is Ljava/util/List;. It's why you can't override methods if the two methods end up with the same name, param types, and return type if you remove all generics.

How can I initialize an element of an array field? [duplicate]

This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
How to initialize an array in Java?
(11 answers)
Closed 3 years ago.
I am having trouble initializing an array element inside my class. How can I assign an initial value at a specific index of my array field?
public class Account{
int num[] = new int[50];
// I can't assign a value like this:
num[0] = 12345;
}
You can initialize the array in the constructor.
Just write:
public Account(){
num[0]=12345;
}
inside your class.
The other possibility is to use an initialisation block(but this is less flexible and difficult du document):
{
num[0]=12345;
}
(Also in your class)

How do i access the object created inside constructor? [duplicate]

This question already has answers here:
What is the difference between field, variable, attribute, and property in Java POJOs?
(11 answers)
Closed 5 years ago.
I want to create an array for each object i create, but i cant get access to it. since its scope is within the constructor.
class Constructor{
Constructor(int vsl)
{
int[] array = new int[vsl];
}
}
If i call this constructor by Constructor c = new Constructor(4);
how can i use array in my code?
Note: i want to specifically create the object inside the constructor and manipulate it using values i get from scanner object.
You can not, that array is scoped and visible only inside of the constructor
what you have to do is declare that array as a member class and initialize it in the constructor:
class Constructor {
private int[] array;
Constructor(int vsl) {
array = new int[vsl];
}
}

Why am I able to modify a List in a method that's out of scope, without returning it? [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
Let's say I initialise my list like so:
public static void main(String[] args) {
ArrayList<String> a = new ArrayList<String>();
a.add("one");
a.add("two");
a.add("three");
a.add("four");
modifyList(a);
}
where modifyList simply changes every value to "one" like so:
private static void modifyList(ArrayList<String> a) {
for (int i = 0; i < a.size(); i++) {
a.set(i, "one");
}
}
If I print the list before and after I call this method, I expect the same original list to appear twice. But for some reason, the ArrayList that gets modified in modifyList is the same as the ArrayList in main.
If I try the same experiment with ints and Strings instead of Lists they do not get modified.
Can anyone explain why?
In Java, parameters are passed by value.
However, you passed a reference to your ArrayList to the method (and the reference itself is passed by value), and therefore the method modified the original list.
If you want to ensure that this cannot happen, you need to pass an immutable list as the parameter.

Categories