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

Doubt about multiple variables in single line on Chapter 3, page 175 (K&B7)

 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chapter 3, page 175, on the last line:

It says that the following is not legal:
int x, y=x+1, z; // illegal: x is not initialized before y uses it

However, x is initialized before y uses it, and therefore it is legal.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roger Jenkins,

First of all, a warm welcome to CodeRanch!

Roger Jenkins wrote:However, x is initialized before y uses it, and therefore it is legal.

That's not true at all! Local variable x is declared before local variable y uses it, but local variable x is not initialized before local variable y uses it. And because a local variable must be initialized before it's used, you'll get a compiler error. And the fact that it's about local variables is very important (and maybe that's not clearly mentioned in this Exam Watch), because instance (and class) variables get a default value (and local variables don't). So when x, y, and z are instance (or class) variables, the line code would compile successfully.

But if it's a statement in a method (or constructor or initializer block or ...), you must initialize local variable x to successfully compile the statement. For example

Hope it helps!
Kind regards,
Roel
 
Campbell Ritchie
Sheriff
Posts: 51418
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Declaring multiple variables on the same line might be permissible Java® syntax, but it is regarded as poor style because of legibility problems.
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Hi Roger Jenkins,

First of all, a warm welcome to CodeRanch!

Roger Jenkins wrote:However, x is initialized before y uses it, and therefore it is legal.

That's not true at all! Local variable x is declared before local variable y uses it, but local variable x is not initialized before local variable y uses it. And because a local variable must be initialized before it's used, you'll get a compiler error. And the fact that it's about local variables is very important (and maybe that's not clearly mentioned in this Exam Watch), because instance (and class) variables get a default value (and local variables don't). So when x, y, and z are instance (or class) variables, the line code would compile successfully.

But if it's a statement in a method (or constructor or initializer block or ...), you must initialize local variable x to successfully compile the statement. For example

Hope it helps!
Kind regards,
Roel


Hi Roel De Nijs and Campbell Ritchie,

Thank you for your words. I feel welcome already!

@Roel:
Thank you for your clear explanation! Yes, your post certainly helps a lot, and I now understand what is being meant. You are right of course that variable x is being declared and not initialized. I had forgotten that that matters with regard to local variables (I had read it somewhere before), but you have reminded me of this fact. That will prove useful for the exam!
However, I have not found any mention or indication in this Exam Watch or in the text that precedes it, that it is about local variables. So I think maybe a erratum is warrented after all?
 
Campbell Ritchie
Sheriff
Posts: 51418
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the JLS (=Java® Language Specification) about forward references and check whether it is legal to declare fields like that.
Also look closely at the context in the book before you notify an erratum; there might be other code around that line showing you have local variables.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote: instance (and class) variables get a default value (and local variables don't). So when x, y, and z are instance (or class) variables, the line code would compile successfully.

Does this hold if they are declared final


Do they still have default values,as they are still instance and class variable?

I don't think so
 
Campbell Ritchie
Sheriff
Posts: 51418
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course they have default values. But you cannot use them. You must initialise or assign final variables before they can be used.
 
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
Roger Jenkins wrote:However, I have not found any mention or indication in this Exam Watch or in the text that precedes it, that it is about local variables. So I think maybe a erratum is warrented after all?

I read the Exam Watch and the preceding text as well and couldn't find a clear mention it's about local variables. So I agree maybe at the bottom of the Exam Watch a note should/could be added to clearly mention this only applies to local variables (and not instance or class variables).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Go through the JLS (=Java® Language Specification) about forward references and check whether it is legal to declare fields like that.

I don't agree! Although the JLS is a very comprehensive document, it's written in a very hard to understand language. Certainly if you are new to Java and even more if you are not a native English speaker. A study guide's main purpose is to explain everything you need to know for a given certification exam and explain it as clear as possible, even for readers with none (or very little) Java experience. So if you have doubts/question about a specific sentence, code snippet,... it's very unlikely the JLS will be able to help you because it's not intended to be a study guide but to be an unambiguous description of how Java will work, what's allowed and what's not.
I never recommend people reading the JLS when preparing for the OCA exam, because I know it's really hard to understand. And it will definitely create more doubts than it will clear. Most of the times it will be a waste of time. You'll get a much better and more clear explanation if you post your doubts/questions in this forum. And if it's really needed for some reason, a link to the appropriate JLS section will be added as well
 
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
Sachin Tripathi wrote:Does this hold if they are declared final


Do they still have default values,as they are still instance and class variable?

In short: final class variables and final instance variables must be initialized explicitly, otherwise your code won't compile! If you want a detailed explanation, just read this post. It covers anything you need to know about the initialization of (nonfinal and final) class, instance and local variables.

Hope it helps!
Kind regards,
Roel
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Campbell Ritchie wrote:Go through the JLS (=Java® Language Specification) about forward references and check whether it is legal to declare fields like that.

I don't agree! Although the JLS is a very comprehensive document, it's written in a very hard to understand language. Certainly if you are new to Java and even more if you are not a native English speaker. A study guide's main purpose is to explain everything you need to know for a given certification exam and explain it as clear as possible, even for readers with none (or very little) Java experience. So if you have doubts/question about a specific sentence, code snippet,... it's very unlikely the JLS will be able to help you because it's not intended to be a study guide but to be an unambiguous description of how Java will work, what's allowed and what's not.
I never recommend people reading the JLS when preparing for the OCA exam, because I know it's really hard to understand. And it will definitely create more doubts than it will clear. Most of the times it will be a waste of time. You'll get a much better and more clear explanation if you post your doubts/questions in this forum. And if it's really needed for some reason, a link to the appropriate JLS section will be added as well


@Campbell Ritchie:
Thank you for the tip and thank you for the link to the Java Language Specification. I think the link will be useful for future reference for me and I have book-marked it.
However, I have clicked on the link and I have tried to read it. At THIS stage, I don't really understand what it says and it indeed seems too overwhelming for me (maybe after the OCA exam?)

@Roel de Nijs:
Thank you for your words and your explanation! I am very much relieved by what you said because it does re-inforce me to go to this forum if I need help.
I think that the prerequisite of understanding what the JLS really says before submitting a post on CodeRanch would indeed have been too daunting for me.
I know I am a still very much newbie, even though I have read the OCA Java SE7 book by Mala Gupta thoroughly and I am in the proces of doing the same thing with the OCA-part of the book by Sierra&Bates (and apart from looking up diverse Java topics on the Oracle site and internet). So thanks again for your encouraging words, because I know now that reading the JLS is for the more experienced programmer, and it is not expected from a newbie to understand it before posting a question (certainly about a study guide).
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Roger Jenkins wrote:However, I have not found any mention or indication in this Exam Watch or in the text that precedes it, that it is about local variables. So I think maybe a erratum is warrented after all?

I read the Exam Watch and the preceding text as well and couldn't find a clear mention it's about local variables. So I agree maybe at the bottom of the Exam Watch a note should/could be added to clearly mention this only applies to local variables (and not instance or class variables).


O.k. I'll re-submit this as an erratum. Of course I will not use my original formulation and I will rephrase it taking into account what you said about initialization and local variables and what you say above. Feel free to modify anything in the submission if necessary.
If it doesn't make the final erratum list, then it's fine by me as well. Either way, it was just my modest attempt as a contribution to make a great book even better.
 
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
Roger Jenkins wrote:O.k. I'll re-submit this as an erratum.

No need to do that! I'll add it as an erratum and add a link to this topic so people can enjoy the whole discussion
 
Roger Jenkins
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Roger Jenkins wrote:O.k. I'll re-submit this as an erratum.

No need to do that! I'll add it as an erratum and add a link to this topic so people can enjoy the whole discussion


O sorry, I already submitted it just now before I read your latest post. Yes, that is even a better solution. Feel free to delete my submission. There's no need then for it of course.
Thanks for adding it as an erratum.
 
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
Roger Jenkins wrote:O sorry, I already submitted it just now before I read your latest post. Yes, that is even a better solution. Feel free to delete my submission. There's no need then for it of course.

No need to delete your submission, I'll simply add the link to this topic to your new submission. That will do the job as well
 
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
Roger Jenkins wrote:So thanks again for your encouraging words, because I know now that reading the JLS is for the more experienced programmer, and it is not expected from a newbie to understand it before posting a question (certainly about a study guide).

Honestly, I'm a Java developer for 10 years now and I don't read the JLS myself at all. I only use it as a reference when I'm in doubt about a rule/concept/... and I can't find a satisfying answer elsewhere (like in source code, a trustworthy resource on the internet,...). So I definitely don't expect a newbie to read (and understand) the JLS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic