• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance variables: need help on potentially simple concept?  RSS feed

 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a public class I have instance variables "e" and "f" above the main method and "g" and "h" below the main method.
In the main method is a constructor for the class.
Below the main method, but sitting before the last instance variable is a method that references the variables above and below it.
In the method is two print statements that prints the sum of each pair of integers.

Why is "h" zero at line 12? I understand that instantiation of all non-static fields happens from top down. If so, why does the method "getThis" recognize int h?
And if it does recognize it, why is it's value zero and not 7. In other words, if the method recognizes int "h" without it being an illegal forward reference, why doesn't it also recognize its value of 7?

The method prints 5 and 6.

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look here http://www.coderanch.com/how-to/java/ForwardReferencing
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blake Edward wrote:In other words, if the method recognizes int "h" without it being an illegal forward reference, why doesn't it also recognize its value of 7?

There's almost never a satisfactory answer to questions like this, because it's: "that's how the designers built it".

As you can see from the link that Joe provided, forward references can get quite involved; and it's highly likely that one of the reasons may have been simply to keep things as simple as possible for the compiler.

And to be honest, you're never likely to run into this situation in real life - and if you do, then the person who wrote the program didn't do their job properly.

In 12 years of writing Java, I've never had cause to use an instance initialiser, or worry about forward references ... and I hope I never do.

An ounce of prevention is worth a pound of cure.

Winston
 
Campbell Ritchie
Marshal
Posts: 56522
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston is right. Initialise all your instance methods in a constructor and you won't have that problem.
 
Blake Edward
Ranch Hand
Posts: 106
4
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate the responses. I was playing around with some code and wanted to know what would happen if I moved the call for the method around in the class. When it did not behave as expected I scratched my head for a while then found out about illegal forward references. It's supposed to be a flawed, hacked together piece of code, similar to an OCAJP exam question. I learned something.
 
Patrick Andrei Tolentino
Greenhorn
Posts: 11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at your order of variable declarations... 'e', 'f', 'g' and a constructor block before 'h' are initialized from top to bottom. invoking your constructor from main method, intializes all your primitive variables, and a constructor block is invoked before 'h', therefore getting '0' as the value of h because it has not been initialized yet when getting the value of 'h' before finishing the getThis() method. after escaping the constructor block, 'h' has now a value of 7, which you did not display as part of your output.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick Andrei Tolentino wrote:take a look at your order of variable declarations...

+1 for your answer, which certainly explains what is going on - but Blake's question was "why?", which is a lot harder to answer without a crystal ball.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!