1. Why canít I say just abs() or sin() instead of Math.abs() and Math.sin()? - The import statement does not bring methods into your local name space. It lets you abbreviate class names, but not get rid of them altogether. Thatís just the way it works, youíll get used to it. Itís really a lot safer this way.
    However, there is actually a little trick you can use in some cases that gets you what you want. If your top-level class doesnít need to inherit from anything else, make it inherit from java.lang.Math. That *does* bring all the methods into your local name space. But you canít use this trick in an applet, because you have to inherit from java.awt.Applet. And actually, you canít use it on java.lang.Math at all, because Math is a ďfinalĒ class which means it canít be extended.
  2. Why are there no global variables in Java? - Global variables are considered bad form for a variety of reasons: Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables), State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state, When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once. For these reasons, Java decided to ban global variables.

See Java Interview Questions and Answers : Part 9 for more Java/J2EE Interview Questions.




Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)