Can you declare a class as ‘private’?
No, if we declare a class as private, then it is not available to java compiler and hence a compile time error occurs. But, inner class can be declared as private.
What are static methods?
Static methods are methods which do not act upon the instance variables of a class. Static methods are declared as ‘static’.
Reason: JVM executes the static methods and then only it creates the objects.
What are static blocks?
A static block is a block of statements declared as ‘static’,some thing like this:
JVM executes a static block on a highest priority basis. This means JVM first goes to static block even before it looks for the main() method in the program.
public static void main(string args)
Output: static block
Is it possible to compile and run a java program without writing main() method?
Yes, it is possible by using a static block in the java program.
We know that the class code along with method code is stored in “method area” of the JVM. When an object is created, the memory is allocated on ‘heap‘. After creation of an object, JVM produces a unique identifier number for the object from the memory address of the object. This reference number is called hashcode number.
To know the hashcode number of an object, we can use hashcode() method of object class as shown here..
Employee el=new Employee();
The Java Heap Space is the memory “container” of you runtime Java program which provides to your Java program the proper memory spaces it needs (Java Heap, Native Heap) and managed by the JVM itself.
Your Java program life cycle typically looks like this:
– Java program coding (via Eclipse IDE etc.) e.g. HelloWorld.java
– Java program compilation (Java compiler or third party build tools such as Apache Ant, Apache Maven..) e.g. HelloWord.class
– Java program start-up and runtime execution e.g. via your HelloWorld.main() method
The Java Heap space is mainly applicable and important for the third step: runtime execution. For the HotSpot VM, the Java Heap Space is split in 3 silos:
– Java Heap for short & long lived objects (YoungGen & OldGen spaces)
– PermGen space
– Native Heap
Now let’s dissect your HelloWorld.class program so you can better understand.
– At start-up, your JVM will load and cache some of your static program and JDK libraries to the Native Heap, including native libraries, Mapped Files such as your program Jar file(s), Threads such as the main start-up Thread of your program etc.
– Your JVM will then store the “static” data of your HelloWorld.class Java program to the PermGen space (Class metadata, descriptors..)
– Once your program is started, the JVM will then manage and dynamically allocate the memory of your Java program to the Java Heap (YoungGen & OldGen). This is why it is so important that you understand how much memory your Java program needs to you can properly fine-tuned the capacity of your Java Heap controlled via –Xms & -Xmx JVM parameters. Profiling, Heap Dump analysis allow you to determine your Java program memory footprint
– Finally, the JVM has to also dynamically release the memory from the Java Heap Space that your program no longer need; this is called the garbage collection process. This process can be easily monitored via the JVM verbose GC or a monitoring tool of your choice such as JConsole