Well, the answer's simple. Java is an object-oriented language, and in a class is the only place it allows you to write code. So you don't need an example to demonstrate that.
If you mean, "why is it designed that way", that's a huge question that we're not going to be able to effectively answer here. I'd suggest reading a tutorial on object-oriented programming to get an idea of how it works.
If you've got some more specific questions we might be able to help you.
Thanks for your reply.But Can I know which tutorial or book you suggest me for understanding this concept like why everything should go into class.what will happen if we will not use class.This question was asked in interview.
irrfan jamadar wrote:Thanks for your reply.But Can I know which tutorial or book you suggest me for understanding this concept like why everything should go into class.what will happen if we will not use class.This question was asked in interview.
Hi Irrfan,
In Java everything goes inside a class because it is how it is meant to be so that OOO features can be utilized fruitfully. You will need to go through the OO Concepts. I would recommend you to buy t Head First Object-Oriented Analysis & Design and go through it and all your basics regarding this issue will get resolved. IF you still face issues then come back here and ask your specific questions.