Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Which event happens first?

 
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am curious to know as which event occurs first initilaization of variables and then object creation or creation of object and then initialization of instance variables.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it would be hard to initialize member variables of an object that does not exist, so it seems logical to me that the object is created first.

But really, it depends on what exactly you mean by "object creation". Do you mean reserving a block of memory to store the object?

This sounds like one of those things that people think they need to know exactly for an exam like OCPJP, but in reality you almost never need to deal with these details at this level; and if you try to look in too much detail, then things start to loose meaning (for example, what exactly does "object creation" mean when you look at machine code level?).
 
BadriNath Ojha
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By Object creation I mean allocation of memory in heap.
Why this Question in my mind is that we can instantiate the subclass variables using the super class constructor and call the overriden method in sub class to set the orivate variable value. Since the super is setting the values and the sub class constructor has not started so object is not there but the variables are initialized.
 
Marshal
Posts: 74651
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks totally wrong. The whole process is described in the Java Language Specification, I think this part.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BadriNath Ojha wrote:Why this Question in my mind is that we can instantiate the subclass variables using the super class constructor


Erm. No you can't.

and call the overriden method in sub class to set the orivate variable value.


Now that you can do, but it indicates very bad design, and would almost certainly result in a runtime error (probably a NullPointerException).

Since the super is setting the values and the sub class constructor has not started so object is not there but the variables are initialized.


I think you may be confusing the business of space being allocated for instance variables with the running of the constructor; and TBH, I'm not exactly sure of the entire sequence myself - I've just never had to worry about it. And if I did, I'd look at the link that Campbell supplied.

It's very difficult to start with if you've come from a language like C or C++ to grasp that Java is a memory-managed language, so things like this usually aren't important. By all means read up on it if you feel you need to, but my general advice would be: DontSweatIt.

HIH

Winston
 
BadriNath Ojha
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Winston, I was going thru the LinkedHashMap impelmentation, there they have done the same thing as above and they are not getting NPE. This was what confused me.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BadriNath Ojha wrote:I was going thru the LinkedHashMap impelmentation, there they have done the same thing as above


Maybe I'm not understanding what you mean by "the same thing", but looking at the LinkedHashMap source code in Java 6, the only variable any of the constructors sets is accessOrder - which is declared in the class itself, not in a superclass.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

BadriNath Ojha wrote:Winston, I was going thru the LinkedHashMap impelmentation, there they have done the same thing as above and they are not getting NPE. This was what confused me.


Like Ulf, I can't see what you're talking about (and I presume you're referring to calling an overridden method from a constructor; because that was what I was referring to when I talked about bad design).

If you're simply referring to super() calls that take values, this is standard practise, because those values are probably defined (and therefore need to be set) in the superclass.

TBH, I wonder if you might not be taking on a bit much by starting out with LinkedHashMap. It is NOT a simple class, and I certainly don't know all its in's and out's. I suspect you could get similar insight from something like ArrayList, which is probably a lot easier to follow.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic