how to execute a program without main().


how to execute a program without main().


PROGRAM First3.java
           public class First3
               {
                  static
                   {
                      System.out.println("It is executing without main()......");
                       System.exit(0);
                        } 
                         } 


                Output –







                1. PROGRAM :
                2. public class First
                3. {
                4. public static void main()
                5. {
                6. System.out.println("It is overloaded main.....");
                7. }
                8. static
                9. {
                10. main();
                11. }
                12. public static void main(String args[])
                13. {
                14. System.out.println("It is invoked by JRE");
                15. }
                16. }


                Output –










                Signature of Method –


                modifier returnType methodName(Argument list)

                {
                               -------------
                               -------------
                               -------------          

                }












                JVM, JRE & JDK


                JVM, JRE & JDK

                v  Format of class file.
                v  Class loading mechanism.

                v  Memory Management etc.


                JVM




                JDK

                ·                  In Java, classes are packaged and distributed in jar format.
                Jar -  Java Archive
                ·                  Sun Microsystems provides classes of core java library through a jar file named “rt.jar.
                ·                  C:\NewJava>jar
                jar -tf rt.jar

                jar -tf rt.jar> a.txt


                To compress
                jar -crf a.jar *
                jar -tf a.jar (to see)

                jar -xf a.jar (to extract)





                The above program in modified form


                PROGRAM first2.java

                public class First2
                {
                public static void main()
                {
                System.out.println("It is overloaded main.....");
                }
                public static void main(String args[])
                {
                System.out.println("Invoked by JRE, invoking overloaded main......");
                main();
                }
                }

                OUTPUT –












                Question : Why command-line arguments?


                Question : Why command-line arguments?

                c:\>java Adder 10    20
                Command line argument

                Result is : 30




                Explanation of:



                [System.out.println();]


                System – It is a pre-defined class in java.lang package

                out - it is a static variable inside System class of type PrintStream(class reference variable) (i.e.) static PrintStream out;
                and PrintStream is a predefined class in java.io package.
                Since it is static that’s why we are calling
                       System.out
                So,  System.out means we are getting the reference to the object of type PrintStream

                println()– It is the method in PrintStream class for Standard output stream.
                So to access println method , we need PrintStream object which we are getting through System.out.

                  So finally its System.out.println---- to produce standard output Stream.



                class PrintStream
                {
                               public void print(String s)
                {
                               -----------
                               -----------
                               -----------
                }

                public void println(String s)
                {
                               -----------
                               -----------
                               -----------
                }
                -------------
                -------------
                }

                                 class System
                            {
                public static PrintStream out;
                -------------
                -------------
                -------------
                }















                STATIC KEYWORD


                STATIC KEYWORD

                In a class we can have 2 types of members.

                i.  Instance members – represents attributes and behavior of individual objects.
                ii.    Class members – represents attributes and behavior of the whole class.






                To understand where we need to use static, see the diagram below-







                v  By default all the members of a class are instance members. In order to associate a member of a class static keyword is used.

                v  static denotes execution of whole class.


                Syntax of compiling –

                javac NameOfSourceFile





                example - 














                Simple Program :- Hello Java Example


                Simple Program :- Hello Java Example

                In this page, we will learn how to write the hello java program. Creating hello java example is too easy. Here, we have created a class named Simple that contains only main method and prints a message hello java. It is the simple program of java.

                Requirement for Hello Java Example


                For executing any java program, you need to
                • create the java program.
                • install the JDK if you don't have installed it, download the JDK and install it.
                • set path of the bin directory under jdk.
                • compile and execute the program.







                Syntax of defining a class –

                class Identifier
                {
                                                             Members definition

                }



                NAMING CONVENTIONS OF JAVA

                1. First letter of each word of a class name is capitalized.
                Example
                String
                StringBuffer
                ArrayIndexOutOfBoundException

                etc.


                1. In case of methods first letter of each word except the first word is capitalized.
                Example
                printf()
                getPriority()
                getKeyMethodMap()
                etc.

                ·                  Functions (in C or C++) represents an operation which can be independent whereas methods represents behavior which cannot be independent.


                Functions (Task)
                Methods (Behaviour)
                deposit( )
                a.deposit(5000);
                b.deposit(7000);
                What is happening
                Way of doing things
                What is happening
                Who is doing.






                Question : Why in Java’s main method return type void is used?

                Figure 1 is compiler-based program which directly calls O/S functions during execution. So, here O/S returns some integer value to acknowledge that whether the application run successfully or not.




                Figure 2 is interpreter-based program which is executed by JRE. Java application has no direct connectivity with O/S. It runs on JRE.

                It is JRE who confirms that all byte codes are valid and do not violate Java’s security restrictions. Any type of rule violation led to error(Compile time or runtime) by JRE.


                Since JRE is boss of everything and itself capable to handle each type of error, it doesn’t expect anything to come back from main(), so we use void in main() method.