• Post Reply Bookmark Topic Watch Topic
  • New Topic

array issues  RSS feed

 
Jeff Sadowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a bit of difficulty working with an array of objects. Below is some test code and actual and expected results.
I'm sure its something really simple but I don't understand what is going on.



output


expected output
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look up what the static keyword means.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, not following expected Java naming conventions makes code surprisingly hard to read. Please be sure to follow the conventions such as naming classes with a capital letter. In other words, Test, not test.
 
Jeff Sadowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Look up what the static keyword means.


If I took out static It would compile but have a run time issue.
I figured it out though. It seems I needed new Objects at each item in the array.
I also fixed the Capitalization as you suggested if that helps.
I fixed the code to look like



now I get the expected output.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But do you understand why you got the original results?
 
Jeff Sadowski
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:But do you understand why you got the original results?

Maybe, but I'd like confirmation.
I think I understand the static a little better in that I guess the class's static objects could be accessed with out the class being instantiated.
That is why I could access them without creating the objects and why when I just took out the static it was complaining that the object didn't exist during runtime.
So another question is why didn't the line

create the three objects and just create pointers(is that what happened?)
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering why you didn’t suffer an Exception. That is because you can call static members of a class on a null reference, if the compiler can definitely identify the class in advance. [I think that was dubious design in the language; the more you exclude from a language, the easier it is to use.]
As Bear has hinted, using static because “it seems to work/compile like that” means you have’t understood what static means, and is, in my opinion, a serious error.
Also why have you made Test a nested class? Unless it is “so I can see how nested classes work”, which is adequate justification for that, I think you ought to move the Test class into a file of its own, with proper encapsulation, i.e. private fields, initialised in the constructor, and getXXX methods as required. Now you can see, when you design your class properly, that you can set up the array like thisI now challenge you to work out how the JVM is supposed to know how to set up that array with all its elements, unless you tell it the values yourself. You can do it programmatically like that, or you can get input from a file, keyboard, etc., but you have to tell the JVM somehow. If you don’t tell the JVM which Test you want where, it is impossible to fill that array. The only way round that is to fill the array with nulls (or for primitives, 0s), which is what happens. So you have to fill the array, otherwise you suffer NullPointerExceptions.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I was wondering why you didn’t suffer an Exception. That is because you can call static members of a class on a null reference, if the compiler can definitely identify the class in advance. [I think that was dubious design in the language; the more you exclude from a language, the easier it is to use.]

On the other hand, this way the compiler is free not to test the reference for nulls. (Moreover, I'd say that the compiler is always able to identify the class at the compile time. Either from declaration, or from casting.)

Had it tested for nulls, you might expect that it actually uses the reference you're calling it on, namely that it will call the static method from the class of the actual instance, not the declared type. Now, that would be confusing! By working even on null references, it is absolutely clear that the actual value of the reference is meaningless.

Not allowing to call static methods using a reference would probably be the best way, though.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Sadowski wrote:So another question is why didn't the line

create the three objects and just create pointers(is that what happened?)

You do realize that an array of type Test[] can hold objects of type Test or any subclass of Test?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:
Jeff Sadowski wrote:So another question is why didn't the line

create the three objects and just create pointers(is that what happened?)

You do realize that an array of type Test[] can hold objects of type Test or any subclass of Test?


Additionally, Test could be an interface or abstract class, in which case it would be impossible.

And not only that, the runtime would have to know what constructor to call and what args to pass it, or else would have to only do it for classes that had a public no-arg c'tor.

In terms of this question, is the same as


So you might as well be asking why Java doesn't create Test objects for those three variables and stick referecnes in the variables. The language could have been written to do that, and I think that's how C++ behaves, at least in some cases, but again, it would only work if Test was a concrete class with a no-arg c'tor. This would make the language and the runtime more complex, and would make more special cases in the behavior. One of Java's core tenets is simplicity and consistency; special cases are the opposite of that.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote: . . . Not allowing to call static methods using a reference would probably be the best way, though.
Put a lot better than I put it
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:You do realize that an array of type Test[] can hold objects of type Test or any subclass of Test?


Just to round off this discussion, and to be more specific: object type arrays don't actually hold objects. They hold references. So, when you initialize an array withthat array is populated with references of type Test (references that can refer to an object that is of type Test or a subtype), all of which are assigned the value null -- which is the default value for references.

Arrays of a primitive type hold values, not references (d'oh) and creation of a primitive array populates it with the default values for that primitive type -- 0 for numeric type variables, false for boolean.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!