Hello, everyone. I’m the teacher who gave the people the nickname doug Tooth.

Last night, Jingdong elder brother yong shared his new JVM interview question in the group. After listening to it, I felt it was very interesting to share it with you

Guys, before you read my analysis, think of your own answers

Are you trying to say

Static methods belong to classes, while non-static methods belong to instance objects

Ha, someone so answered, yong Brother said not satisfied. That’s one sentence away from the right answer. If you say it, you know it. Which one?

finishing

How does the JVM call Java methods? Leaving call_stub, entry_point, and execution stream out of the equation, the pseudocode looks something like this: Build the runtime environment -> call Java methods

What does building a runtime environment do? Whether to trigger class initialization, find method objects by symbolic references, cache direct references to method objects, create stack frames, copy parameters…

The JVM follows this process for both non-static and static methods. The difference is in building the runtime environment. If a non-static method is called, the build runtime does one more step: assign a value to the this pointer. To help you understand this, I have extracted this code from my handwritten JVM.

Here’s the answer to the question:

  1. If a static method is calling a nonstatic method: the first parameter of a nonstatic method is implicit and cannot be passed, so it cannot be called. In fact, you have to support it is not impossible to do, will be a bit more trouble, to change a lot of mechanism. Because static methods are common to all Java objects. But if it does, the distinction between static and non-static is meaningless.
  2. If a non-static method calls a static method: can be called. Because non-static methods are called, the this pointer is not needed. Actually even if want, also some give.

Did you GET it?

More than expected

If I came in for an interview, this question would not end, I would ask: what do you think other languages, such as C, C++, Python, Golang do? Why ask? Because even if you can answer the above answers, it doesn’t really make you stand out. Chances are, a lot of people can answer it.

It would be nice if I asked you to answer. It shows that you have a three-dimensional understanding of technology. But many Java Coder technologies are fragmented. That means Java is all they have in mind. You need to get him thinking by asking questions.

So what does Python do? Calling python methods requires manually passing the this pointer. Since it is not an implicit argument, this question can be easily answered in Python.

What if it’s C? Look at the code

No error, right? This does not mean that static methods can call non-static methods in C. In C, static is not used to define non-static methods; it is used to limit the scope of methods. That is, the static modifier precedes the method, which can only be called in the current file. Because C is a process-oriented language. The method here is more appropriately called a function.

What about C++? Look at the code

The result, and the principle behind it, is the same as in Java: non-static methods can call static methods, and static methods cannot call non-static methods because of the this pointer.

Recommended reading

  1. How to become a tech guru? How to gain architectural experience?
  2. STW, who’s been bothering you for most of your life, is finally graduating today
  3. From hotspot source code level analysis of Java polymorphic implementation principle

conclusion

I am son tooth teacher, a clearly can rely on the face to have a meal, but unluckily rob everyone rice bowl of man. In-depth study of THE JVM, Linux kernel, Windows kernel, focus on sharing super core dry articles. Also, my entrepreneurial tips. Operation public number: hard nuclear tooth. My core article and entrepreneurial experience can be seen here, the original article has been a lot of a lot. Of course, BUT also my handwriting JVM small class, if you want to advance, want to lay a solid foundation, welcome to learn with me.