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

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)

Related

Java HashSet remove item by its hash code [duplicate]

This question already has answers here:
Remove object from ArrayList with some Object property
(5 answers)
Remove objects from an ArrayList based on a given criteria
(10 answers)
How to remove specific object from ArrayList in Java?
(17 answers)
Closed 1 year ago.
I have a class with its own hashCode() method. I am adding this class to a HashSet. How can I remove an item by its hashCode, without knowing the object itself?
For example, if I have the following code
HashSet<Data> set = new HashSet<>();
set.add(new Data(10, 5));
...
class Data {
public int importantVal;
public int notImportantVal;
//... constructor ...
#Override
public int hashCode() {
return importantVal;
}
}
and I knew the importantVal of a Data object, but not the object itself. How would I remove it? set.remove(10) does not work.
Best solution I can think of is to also override equals() to return if importantVal is the same, and then do set.remove(new Data(10, anyPlaceholderValue))

If String in Java is of final Class then how come we change the value of it? [duplicate]

This question already has answers here:
How does the "final" keyword in Java work? (I can still modify an object.)
(20 answers)
What is the point of "final class" in Java?
(24 answers)
Closed 3 years ago.
Even though String is a final class we can change the value of it like:
String A = "hello";
And in next step:
A = "World";
Here A will be changed.
Whereas in case of a final variable we can't do it like:
final int a =10;
a = 13; //This Will Give Error
This would be a contradiction.
This is because when the class is final it means the methods of the class cannot be changed or overridden. If a field is final, then the value cannot be changed after the initial value has been assigned.

How to Access Variable's Memory Address In Java? [duplicate]

This question already has answers here:
hashCode uniqueness
(7 answers)
What is an object's hash code if hashCode() is not overridden?
(12 answers)
Does hashcode number represent the memory address? [duplicate]
(9 answers)
How can I get the memory location of a object in java?
(4 answers)
Closed 3 years ago.
I wrote this code and want to see memory location of 2 objects that i create from one class and make instance of one specific variable.
public class StaticFields {
int a = 12;
int b = 235;
public static void main(String[] args) {
StaticFields obj = new StaticFields();
int a = obj.a;
StaticFields obj2 = new StaticFields();
int c = obj2.a;
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(c));
}
}
why the "identityHashCode" of "a" and "c" is the same ?
Thanks.
Both the integers carry the same value, 12.
Since Integers are cached (for values up to 127 from -128), the hash code value of both objects returned are same.
This is not true for b since its value is greater than 127.

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];
}
}

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

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];

Categories