• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some questions regarding initialization of fields

 
Prasanna Lakkur Subramanyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was studying about how fields get initialized. From what I know so far, I've prepared this diagram to help me study.


1. Firstly, is my understanding so far correct? (please look at the image)
2. Second, I understand that instance variables get initialized to 0(or their default value) when they're first encountered(before their super classes are initialized). Is the same true for static variables as well?
3. When are methods available for use? In every book that I've read, they always mention when variables should or should not be used but they never talk about methods. Are all the methods of an object available and ready to be called as soon as the object creation has started?
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Lakkur Subramanyam wrote:1. Firstly, is my understanding so far correct?

Yes,as far as I think you are almost correct,just a little bit correction is needed,
All instance variables don't have their default values as 0.

when an instance of subclass is created,it will create all of its instance variable as well as all the instance variables of its all super classes upto Object classes and place their default value in them accordingly.
Now when constructor of subclass is invoked which in return invokes constructor of its super class and this follows upto Object class ,then the Object class set the explicitly defined value to their variable,(and do all other stuffs as mentioned in their constructor),then execution returns to its immediate subclass,then it set explicitly defined values to variable,(and do all other stuffs as mentioned in their constructor),

this follows until they reach the last subclass's constructor and do the same things

Prasanna Lakkur Subramanyam wrote:2. Second, I understand that instance variables get initialized to 0(or their default value) when they're first encountered(before their super classes are initialized). Is the same true for static variables as well?

Yes every variable gets its default values(until they are local variables,and you don't define the value explicitly)


Prasanna Lakkur Subramanyam wrote:3. When are methods available for use? In every book that I've read, they always mention when variables should or should not be used but they never talk about methods. Are all the methods of an object available and ready to be called as soon as the object creation has started?


Not very sure about this,let others answer this

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static fields and methods inheritance shows really strange behaviour. Static is not meant for inheritance. As a rule of thumb, always refer to static fileds and methods by prefixing them with the class name, e.g. MyClass.getX(), MySubclass.getX(). This should make everything clearer.
 
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 Prasanna Lakkur Subramanyam,

First of all, a warm welcome to CodeRanch!

Prasanna Lakkur Subramanyam wrote:1. Firstly, is my understanding so far correct? (please look at the image)
2. Second, I understand that instance variables get initialized to 0(or their default value) when they're first encountered(before their super classes are initialized). Is the same true for static variables as well?
3. When are methods available for use? In every book that I've read, they always mention when variables should or should not be used but they never talk about methods. Are all the methods of an object available and ready to be called as soon as the object creation has started?

I think it's very hard to determine based on 1 object creation (new SubClass();) and some circles to determine if your understanding is correct. I don't know anything about the class hierarchy of SubClass and have no idea about what these circles are representing (classes, objects,...).

In this thread you'll find an answer to probably all of your questions (even about when methods are available). About the static variables: here you'll find how code with static variables and static initializer blocks is executed. And here you'll find an overview about the initialization for class (static), instance and local variables (even when they are marked as final).
So carefully read all those topics from top to bottom and if you still have some doubts/questions, feel free to click on the Post Reply button and let us know

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
A.J. Côté wrote:As a rule of thumb, always refer to static fileds and methods by prefixing them with the class name, e.g. MyClass.getX(), MySubclass.getX(). This should make everything clearer.

That's indeed a very good practice when you are writing (production) code. But for a certification exam, it's pretty useless because you still need to correctly assess code snippets where class fields/methods are not prefixed with the class name
 
Prasanna Lakkur Subramanyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for the replies!

@Roel De Nijs - Haha you have a good point there. I did not specify what the circles meant. The circles are the objects. There are only three object in total. The three circles on the left represent the objects during the static initialization phase. The ones on the right, during the instance initialization phase. The links helped for sure but I still didn't get the specifics about the methods and when we can call them.

I was thinking about this for some time now. And I've come up with a sort of an understand of how it might work.

-> The Class object for the particular class is loaded before anything else.
-> There is only one copy of the methods in the memory and every object just passes the this reference to the method.

So, if the methods actually exist in the Class object(and this is where I'm not too clear about things), then the methods will obviously be available before execution of any code in that particular class.
 
Henry Wong
author
Marshal
Pie
Posts: 22092
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Lakkur Subramanyam wrote:
I was thinking about this for some time now. And I've come up with a sort of an understand of how it might work.

-> The Class object for the particular class is loaded before anything else.


Well, the Class class file is, of course, one of the very first things that is loaded by the JVM. And as for the Class object (for your class), that is instantiated while your class is loaded.

Prasanna Lakkur Subramanyam wrote:
-> There is only one copy of the methods in the memory and every object just passes the this reference to the method.

So, if the methods actually exist in the Class object(and this is where I'm not too clear about things), then the methods will obviously be available before execution of any code in that particular class.


How methods are stored is an implementation detail -- and is not really specified by the JLS.... however, yes, you are correct in that the methods of a class can't execute until the class file for the class is loaded.

Henry
 
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
Prasanna Lakkur Subramanyam wrote:I did not specify what the circles meant. The circles are the objects. There are only three object in total. The three circles on the left represent the objects during the static initialization phase. The ones on the right, during the instance initialization phase.

And how do you get 3 objects? I only see one object being created (using new SubClass();). So it's impossible to have 3 objects in total. There should be only one object, not three.

Prasanna Lakkur Subramanyam wrote:-> The Class object for the particular class is loaded before anything else.

True! Loading the class file is indeed one of the things that happens first. And one of the next steps the class will be initialized (= executing its static initializers and the initializers for static fields (class variables) declared in the class). When these steps exactly happen are defined in different sections in the JLS. I'll provide a link just for completeness (this is definitely not on the OCA exams): 12.2. Loading of Classes and Interfaces and 12.4. Initialization of Classes and Interfaces

Prasanna Lakkur Subramanyam wrote:-> There is only one copy of the methods in the memory and every object just passes the this reference to the method.

So, if the methods actually exist in the Class object(and this is where I'm not too clear about things), then the methods will obviously be available before execution of any code in that particular class.

I have no idea about how methods are stored (luckily that's also not on the OCA exams ). But once your class is successfully loaded and initialized, you can access its static methods and fields, create new objects and access instance methods and fields on these objects.

Don't forget to read all aforementioned threads/topics as well, because they contain lots of valuable information about these concepts as well.

Hope it helps!
Kind regards,
Roel
 
Prasanna Lakkur Subramanyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Roel De Nijs: ha-ha yes! One object and two super classes(and no, they're not direct super classes). Got me there.

Yes. I will read from all the links posted. Finally, methods make sense now.

Thanks everyone for the replies!
 
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
Prasanna Lakkur Subramanyam wrote:@Roel De Nijs: ha-ha yes! One object and two super classes(and no, they're not direct super classes). Got me there.

Just to make sure it's absolutely clear, this small code snippetClass C has three (not two) superclasses: B, A, and of course Object. If you create a new C instance (using new C();), only one object will have been created, not 4 (or 3 or 2), just one!
 
Prasanna Lakkur Subramanyam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Thanks for the extra explanation!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic