Clarification about Some Java Questions

1  A static method can called only static field and another static method but MAIN() is also static and we can call non-static method and fields inside MAIN()?Justify it?

Misconception. A static method can call non-static methods also, but it needs an instance variable to invoke the method. Lets say if a static method could invoke a non-static method without a reference variable. In this case it woud be impossible for the compiler or the VM to resolve on which instance the method has to be invoked on, since static context is not coupled to instances.

2 How many type of modifier a class can have and what is the default modofier?

Public and package. It does not make sense to make a outer class private or protected, does it?

3 Why constructor can't be abstract or static?
If a constructor was abstract how would you create an object of it?

Well it can't be static since as I mentioned before static context is not coupled to instances and the raison d'etre of a constructor is to create instances.

4  Why a java file can't have more then one public class?

This is a small optimization technique to make the life of the compiler and the VM easier. Imagine if you could declare several
public classes within the same compilation unit(file), how difficult it would be for the poor compiler to search for it.

Java Tips by : Schizophrenicpsychosis

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.