It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
And if that weren't confusing enough, you have a class called ag. Please have a look at the old Sun style guide about naming things. Yes, naming conventions are important, not just things for us mods to get annoyed about You can introduce errors inot your code by poor naming of things.Daniel Demesmaecker wrote:. . . your class, instance variable and array are all called age, . . .
Daniel Demesmaecker wrote:And as for the location of the object, you're not trying to print the variable age, but the instances of Age. To do so, you'll need a toString method or a getter to get the value of the instance variable.
Since you didn't mark the instance variable private you could also use age[y].age
Also, just a piece of advice, don't call everything age, your class, instance variable and array are all called age, it's hard to keep track about what your talking. An array is general pluriel.
So if you set on using thoose kind of names, which I don't recomend change it to Age (class) myAge(instance variable) and ages (array)
rian bron wrote:
yeah but why doe's it prints null with just saying " System.out.println(age[y]);"?
rian bron wrote:
inside the Age array there are 10 objects of Age who consists the age ( as i set at the constructor)
rian bron wrote:
doesn't it makes sense the printing the object in the array who has only 1 variable should print this variable value?
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
It does actually print null for half the elements in that array, not a location. It isn't usually possible to print an object's location in memory in Java®Daniel Demesmaecker wrote: . . . It dosn't print null, it prints the objects location. . . .
I would have thought an object with one field is useful as an example whilst learning. There surely are other uses for one&‑field objects, too.It simply dosn't make sence to create an object with a single instance variable. . . .
Campbell Ritchie wrote:It does actually print null for half the elements in that array, not a location. It isn't usually possible to print an object's location in memory in Java®
It's good to be able to use someting, it's better to understand how it works.
www.goanation.net
That is the hash code after the “@”. If you look in the Object documentation, you will find that the toString() method prints class name then @ then hashcode. Since that method is first declared in Object, every object in Java® has a toString() method. You shuld really always override toString, and in many cases hashCode() and equals() too. You can get the same hash code as returned from the unoverridden method with System#identityHashCode(). Object#hashCode() might return a location, or it might not; if there has been a round of garbage collection, however, most objects will have moved from their original location and identityHashCode() might give you their old locations.Daniel Demesmaecker wrote:. . . trying to print an object and it returned it's location . . . is it safe to asume that when there is a toString method it's is being automaticly called when trying to print the object? . . .