Java Deep

Pure Java, what else

Can you call non-static method from a static?

There are questions on a Java technical interview that even the most entry level junior is expected to give the right answer for. Since I am facing candidates who are not that junior I do not even bother most of the times to ask those questions. I assume that the candidate knows the correct answer. Some time however there are some candidates who I feel from the start they are juniors and to cut the interview short not wasting his/her and my time I ask some of those simple questions. The answers usually reveals the real level of knowledge and we can get to an agreement in short time about the assessed level.

Sometimes I also meet candidates who not only simply do not know the answer but give wrong answer. To know something wrong is worse than not knowing. Out of these very few even insists and tries to explain how I should have interpreted their answer. That is already a personality problem and definitely a no-go in an interview.

One such simple question is:

Can a static method in a class call a non-static method of the same class?

If you know Java a little bit you know the answer: no, it can not. A static method belongs to the class and not the instance. It can even be executed using the name of the class directly without any instance of the class. It can even run when there is not even a single instance of the class in the whole JVM. How could it invoke a normal method that runs attached to an instance?

But then again: the answer from one candidate this time was: yes. And he even started to explain that it may happen that the static method has access to an instance. It may get an instance as a method argument and through that reference it can call an instance method. That person was right. It did not, however, mended the fact that he did not know Java good enough, but as a matter of fact in this very specific question she was right.

What is the morale of the story? You tell.

4 responses to “Can you call non-static method from a static?

  1. tamasrev November 25, 2015 at 4:46 pm

    The question was too board, I guess🙂
    I’d say yes too. Like, it’s a stupid example but it’s good for the compiler: you create an instance in the static method, then you invoke a method on this instance. Voila.

    Like

  2. csorbazoli November 25, 2015 at 9:32 pm

    Yes, you usually do it from the public static void main method of a class:)
    First instantiate the class then call a method of it.
    But the trivial answer would be “no”, unless you are expecting to have a tricky question;)

    Like

  3. Kofa December 4, 2015 at 9:22 pm

    It’s a bit like the questions from school: ‘what’s the next number in the sequence’? I have to agree with the others that the answer was acceptable and logical, it’s just not what you had in mind; you wanted to hear the ‘canonical’ answer (‘no’).

    Like

  4. tvk December 29, 2015 at 4:59 pm

    The morale for me:
    A healthy and professional conversation is more important than the answer itself.
    If the interviewer blindly insists to some putative ‘right answer’, that is also a good sneak peek into the company culture.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: