Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Possible errata on page 20 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Mitja Kancler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, there is a tiny mistake on page 20, where the text says:

Order matters for the fields and blocks of code. You can't reffer to a varibale before it has been initialized.


and the code is



I think, that the word initialized sould be replaced with declared. Instance variables has default initialization, so i belive that is not an issue.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mitja Kancler,

First of all, a warm welcome to CodeRanch!

Mitja Kancler wrote:I think, that the word initialized sould be replaced with declared. Instance variables has default initialization, so i belive that is not an issue.

You are correct! Instance (and class/static) variables are assigned a default value if not explicitly initialized. The following code snippet compiles successfully, although line1 refers to the instance variable name before it is initialized on line2. So that confirms your statement

But even replacing "initialized" with "declared" is not 100% accurate. You can indeed not refer to an instance variable before it is declared if you are using the simple name of the variable (as illustrated in this code snippet)But if you qualify the instance variable with this., you can refer to an instance variable even before it is declared. The Person3 class compiles without any errors and prints null

Hope it helps!
Kind regards,
Roel
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In those examples, that is true. However, consider the following:



The variable is declared, but not initialized inside a block. Which is consistent with what the text says.

The good thing is that just the fact we are discussing this tiny detail shows you understand it!
 
Mitja Kancler
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your answers!

I didn't know, that with this. you can forward reference a variable.

I have have a question about the second answer:



I belive that the variable name is not instance variable, so it must be local. Where does it get created? Is it created on the stack even thou there was no method call?

I also have a question about question 14 on page 46:

i was compiling the code and the package name didn't matter, as long as i was compiling with: javac named\A\Bird.java (on windows). Am i missing something?

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitja Kancler wrote:I belive that the variable name is not instance variable, so it must be local. Where does it get created? Is it created on the stack even thou there was no method call?

You are correct! The variable name is indeed not an instance variable, nor a class (static) variable. It is a local variable and its scope is the instance initializer block. That means that the variable will exist only during the execution of this instance initializer block. So you can have multiple instance initializer blocks defining a variable name and the code will still compileOnce the instance initializer block is executed, the variable name will be created on the stack (and the object it refers to, will be placed on the heap).

Finally, keep in mind that a local variable must be initialized before it is used/accessed. So the code snippet from your previous post will not compileIn this topic you'll find an overview about the initialization for class (static), instance and local variables (even when they are marked as final).

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mitja Kancler wrote:I also have a question about question 14 on page 46:

i was compiling the code and the package name didn't matter, as long as i was compiling with: javac named\A\Bird.java (on windows). Am i missing something?

Could you create another topic for this question? That will make it much easier to keep an overview (for other ranchers as well) and less confusing because answers to different questions are not interfering with each other. Thanks for your friendly cooperation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic