I have some basic questions with Java langauge.
1. In Java, why the main() fn should be inside some class. Main is a place where objects interact, so it can be anywhere as in C++. Why does it restricts us to place the main inside some class.
2. Why Java restricts us to dynamic memory allocation. I mean, why cant objects be created at compile time and only at Run time.
I need a justifying explanation
The fundamental unit of structure for Java is the object which is defined by a class abstract data type. Afterall, Java is an Object Oriented Programming language. Everything is a java program is an object (except the primitive data types). It makes sense and, when you think about it, is imperative for this language to include its method main (and all other code) within the definition of a class which defines an object.
Not all allocation is dynamic - any static member is created in the program stack.
I propose two main design decisions in the answer to your question: to provide a uniform process of creation of objects and reference to those objects and memory management.
All objects are created through a call to new. All objects are accessed through reference.
Too many people are unable to learn, or get messed up by, memory management in languages such as C and C++. A key design feature of Java is that the language provides memory management, behind the scenes, in the JVM. Makes sense, since the organizing element of program design is objects, and the performance of instances of objects which are created and destroyed.
Yet another design decision which may play a part in this is that since Java is a write once, run anywhere language, "compilation" is the process of creating "byte code" which is then interpreted execution to the Java Virtual Machine. Static binding is operating system specific. That binding should be implemented by the JVM, not the compiler.
I expect you can access MANY articles and papers on these design decisions on the Internet. Take a look at the Java Developer site.
By Frank in forum Database
Last Post: 03-05-2003, 02:37 PM
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center