aspose file tools*
The moose likes Beginning Java and the fly likes Is this a good way to explain - Cannot make a static reference to the non-static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is this a good way to explain - Cannot make a static reference to the non-static method" Watch "Is this a good way to explain - Cannot make a static reference to the non-static method" New topic
Author

Is this a good way to explain - Cannot make a static reference to the non-static method

Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
I was asked this question where it says you are not allowed to put non-static method inside a static code. I gave them the explanation below.
Was it okay ? Please tell me if it needs to be improved or if its wrong -



Thank you.

Java Newbie with 72% in OCJP/SCJP - Super Confused Jobless Programmer.
I am a "newbie" too. Please verify my answers before you accept them.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Andy Jack wrote:I was asked this question where it says you are not allowed to put static code inside a non-static method. I gave them the explanation below.
Was it okay ? Please tell me if it needs to be improved or if its wrong -



Thank you.



I think that you may have misinterpreted the question -- because you can't have a method declared in another method. And this is true regardless of whether a method is static or not.

Are you sure the question wasn't about context -- and how you can't call an instance method from a static method (ie. a static context)?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
Henry Wong wrote:

I think that you may have misinterpreted the question -- because you can't have a method declared in another method. And this is true regardless of whether a method is static or not.

Are you sure the question wasn't about context -- and how you can't call an instance method from a static method (ie. a static context)?

Henry


I made a mistake - flipped the static and non-static part. I edited the question to show what I actually wanted to say. Is the new answer okay ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Andy Jack wrote:
I made a mistake - flipped the static and non-static part. I edited the question to show what I actually wanted to say. Is the new answer okay ?


First of all, you do know that I quoted your original question in my last response -- and you did more than flip the order. You wrote a new explanation... so, I am assuming that this second answer wasn't what you gave as a response?

Anyway...

Andy Jack wrote:I was asked this question where it says you are not allowed to put non-static method inside a static code. I gave them the explanation below.
Was it okay ? Please tell me if it needs to be improved or if its wrong -



Thank you.


If I got this as the response to an interview question, my followup question would be ... How does a static method trying to call a non-static method lead to "a method can't be static and non-static at the same time"? aren't methods declared to be either static or not? Also, since a non-static method can call a static method, does that mean that there are cases where a method can be both static and non-static?

Of course, these followup questions are mostly in jest. The point is, you have to be clear on your terminology -- as if you don't follow the norms, they can be easily turned on you.

Henry
Andy Jack
Ranch Hand

Joined: Nov 22, 2012
Posts: 257
Henry Wong wrote:

Also, since a non-static method can call a static method, does that mean that there are cases where a method can be both static and non-static?
Of course, these followup questions are mostly in jest. The point is, you have to be clear on your terminology -- as if you don't follow the norms, they can be easily turned on you.

Henry


Gotcha moment ! What is the reason for the error ?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

Andy Jack wrote:
Henry Wong wrote:
Also, since a non-static method can call a static method, does that mean that there are cases where a method can be both static and non-static?
Of course, these followup questions are mostly in jest. The point is, you have to be clear on your terminology -- as if you don't follow the norms, they can be easily turned on you.


Gotcha moment ! What is the reason for the error ?


So... now you are not sure of your answer? ... Anyway, I am going to let you do research on this. Why? Because, the answer isn't very succinct. Any answer that you give during an interview can lead to very details discussions on why Java behaves the way it does.

Do a bit a research on the difference between static and non-static methods. Particularly what is present in one, but not the other. Also, take a look at the "this" variable too.

With this information, try to answer... Why a static method can't call a non-static method? Why a non-static method can actually call a static method? And also, under what conditions can a static method method, actually call a non-static method?

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
To go back to what Henry said about terminology. We use jargon a lot, because jargon is concise. If you use the jargon incorrectly, you will not do well at the interview because you will be perceived as unable to communicate with the rest of the team.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this a good way to explain - Cannot make a static reference to the non-static method