This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

At what point do instance variables get initialized?

 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a class and a subclass which have the same method, and an object is created based with the reference type being the superclass, which has a constructor which executes the method, it doesn't initialize the variable in the execution of that method, but if I make the object and reference type equal, it does.  

So, at what point is it initialized?  

Here is an example.  

 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Improperly formatted code is very hard to read. If there are bugs, then improperly formatted code makes them harder to find. Be nice to people trying to read your code and format it properly. Do yourself a favor and properly format your code so you won't have a harder time finding bugs.

Most IDEs have an autoformat feature that formats code to commonly used standards. I encourage you to use an IDE that has that feature, like Eclipse or IntelliJ IDEA or NetBeans.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never call a method that can be overridden from a constructor. You'll run into that kind variable initialization problem quickly.

See what happens if you put this statement on line 18:
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code syntax is just fine here.  
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Never call a method that can be overridden from a constructor. You'll run into that kind variable initialization problem quickly.

See what happens if you put this statement on line 18:



It was more for conceptual and exam purposes.  It calls it correctly when doing the method, but when creating the constructor, it recognizes that the variable exists, but makes it 0.  It was initialized after it was created, so I was just wondering why it wouldn't work.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:The code syntax is just fine here.  



Actually, it's not. What you posted doesn't compile.

Java is case-sensitive. The instructor in my first programming class was very strict on proper formatting and indentation, probably because he had industry experience and knew the importance of readable code. Either standards have fallen drastically or well, I'll just leave it at that...
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Nathan Milota wrote:The code syntax is just fine here.  



Actually, it's not. What you posted doesn't compile.

Java is case-sensitive. The instructor in my first programming class was very strict on proper formatting and indentation, probably because he had industry experience and knew the importance of readable code. Either standards have fallen drastically or well, I'll just leave it at that...



I fixed that part.  The rest of it is fine.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:It was initialized after it was created, so I was just wondering why it wouldn't work.


When you call print() on line 3, it does execute the statement on line 23. However, the instantiation process for B hasn't completed at that point and the value of the instance variable B.number is still 0. It will only be initialized after the superclass A constructor has completed.  Again, you should never call a method from a constructor if that can be overridden by a subclass. The only type of methods that are safe to call from a constructor are private methods and final methods.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:I fixed that part.  The rest of it is fine.


If someone tells you your fly is down, you'd look down and zip up, right? And if someone tells you your tie is crooked or your shoelace is untied, you might thank them and straighten up your tie or bend down to tie your shoelace. Or you might not. That's your choice.

Just some friendly advice though, if you're planning on making programming something more than just a passing hobby or a class you need to pass: do yourself a favor and heed the advice of people who have experience.
 
Rancher
Posts: 3369
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Nathan Milota wrote:It was initialized after it was created, so I was just wondering why it wouldn't work.


When you call print() on line 3, it does execute the statement on line 23. However, the instantiation process for B hasn't completed at that point and the value of the instance variable B.number is still 0. It will only be initialized after the superclass A constructor has completed.  Again, you should never call a method from a constructor if that can be overridden by a subclass. The only type of methods that are safe to call from a constructor are private methods and final methods.



It resembled an exam question, which did it in that order and asked what the value of the variable would be on the line the object is created, which was 0.  I was just wondering why it was able to see the variable existed, and use it in a method, but yet not initialize it since it was a class variable.   By the time you call the method in the class, it is correct, but I was wondering at which point it became initialized since it isn't once the class is executed.
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cleaned up the code as much as I can.  It is indented and still works just fine.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:I was just wondering why it was able to see the variable existed, and use it in a method, but yet not initialize it


"it" was able to see the variable because execution flow goes like this:

constructor B()
   before anything else, call super(), which is the constructor A()
   super() is implicitly called first in constructor B() since there is no explicit call to a constructor of the superclass A.

constructor A()
   super() called, executes Object() constructor
   print() called -- this is a polymorphic call since class B overrides A.print()
   at this point, B has not been fully initialized and B.number is still 0
   B.print() is executed and displays 0 for B.number
   constructor A completes, execution goes back to constructor B()

constructor B() completes, now B.number is initialized to 7

A reference to the B object just created is assigned to the reference variable a

since it was a class variable.


Technically, number is called an instance variable, not a class variable. Class variables are declared static. Be precise in your terminology so there are not misunderstandings.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nathan Milota wrote:It is indented and still works just fine.


Java doesn't care about indentation. It's for people who have to read the code that proper indentation and formatting makes a difference. It's just common courtesy to present code that's easy to read if you're asking people to look at it and help you with your programming  problems. Is this really such a difficult concept to understand?
 
Nathan Milota
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Nathan Milota wrote:It is indented and still works just fine.


Java doesn't care about indentation. It's for people who have to read the code that proper indentation and formatting makes a difference. It's just common courtesy to present code that's easy to read if you're asking people to look at it and help you with your programming  problems. Is this really such a difficult concept to understand?




Well, I mean, I could understand a bit more if it was a long class full of code.   It was about 5-10 lines work that takes less than a minute to read, so I was just throwing it together for a quick question, and didn't even use an IDE.   I thought people would kind of get the jist of it.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
( ლ.–) *sigh*  

If you don't get it, you don't get it. ¯\_(ツ)_/¯

Good luck with your studies then.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!