I am having a very difficult time understanding this task. I've worked on it for about 3 days now and I'm still a little confused.
I think I understand the concept of using the "this" keyword, though I'm still a little fuzzy on it. It wasn't explained well at all.
But what I'm really confused about is why in this task the person object is created BEFORE the Person class. This makes things very confusing, even if it's acceptable to the compiler.
Both the person object and the Person class are created in the Solution class, I get that. I also understand that the Person class is static, thus all objects (instances of Person) will inherit the non-static variables declared there. Is this correct?
But I got majorly confused about why the person object was being created, and then the adjustAge method is being called when the method wasn't created yet!
It's not created until below!
Can someone explain this to me. It was my assumption that the compiler would error when trying to create this object and run this method call (if reading the program from top to bottom) because they are BEFORE their creation. Does this make sense?
So now I'm assuming the order doesn't matter, but I was hoping someone could explain this to me.
Thank you for your answers!
Why is the person object created before the Person class???
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa
11 October 2021, 12:01
In the programming language C it is like you assume here. You can't use functions until they are known to the compiler, and that's not until the compiler reaches the point of declaration in the source code.That's why they invented such great things as prototypes there. With Java it is not like that.
Java does not load classes into memory just like that, but only when they are needed (when a static member of a class is accessed). For this there is a subsystem of the JVM, the classloader. This loads the class into memory, analyzes it, checks for correctness and creates a class object. This contains all possible information about the loaded class, about its methods and their modifiers, constructors, fields and whatever else. So when the class is loaded, everything defined in it is also known to the compiler, immediately from the loading of the class.
As said above, the Person class is loaded when a static member is called, e.g. the constructor (it is implicitly static). The methods are now known to the compiler and you can call these methods on the created object. This is where this comes in. this refers to the current object on which a method is called. Here the Person class has a method adjustAge, if you call it on the person object, then this refers to the person object and therefore also to all fields of this object. If you create a new Person object jimmy, and call adjustAge on jimmy, then this references the jimmy object.
Back to the classloader. When you compile this task, you get two class files, a Solution.class and a Solution$Person.class file. You can see that the inner classes are not loaded with the outer class either.... You have to access them a static member first to load them into memory.
+7
Hoist
24 January, 22:21
Great Question --- Great answer ) This explanation of the --- this -- keyword and the compiler explainer --- order of execution stuff -- is really a winner !
0