Answer the question
In order to leave comments, you need to log in
How to iterate over an object in a loop?
Hello!
Java is my first strict language and I'm frankly puzzled..
// main
User u1 = new User("Jack", 41);
u1.info();
class User {
public String name;
public int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public void info() {
// как обойти в цикле все свойства текущего объекта?
}
}
var obj = { name: "Jack", age: 33 };
Answer the question
In order to leave comments, you need to log in
It is impossible to bypass fields without reflection, and it is impossible to create an object without a constructor.
Here you are not here.
Static typing however.
You can bypass the properties of an object in a loop through reflection
. But it’s better not to do this. Because it is not necessary for your task. Because static typing!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question