Wednesday, March 19, 2014

FIRST JAVA PROGRAM, Part1 - Writting First Class

Before you write your first program, you should know the basic class structure written in Java.


  1. Apart from this, you can use package statement .
  2. After package statement you can use import statement, if you want to import classes from different packages. 
  3. While building application, using package and import is one of the essentially constraint.
In coming posts i will explain what is use of package and import statement and why we use these?
i will explain about all class members.






After looking at this structure, you must be thinking is that always necessary to write this way.Can't we write something like this only :

public class Abc {

}

Answer to this question is , Yes , we can write this and our compiler will not complain anything. But simply writing this class does not make any sense. I am representing this just for demonstration.

How to write and compile java program from command prompt :
  1. Open any text editor, and write these lines.
  2. Save that file in local disk where ever you want .
  3. While saving the file you should save it with the name of class declared with public and use the extension .java.
  4. Now open system's command prompt and reach to the folder where you have save .java file.
  5. Now use command javac <name of the .java file> like javac Abc.java
  6. To use this command from any where(inside any directory ) you must set path variable and for that you can follow my post here.
  7. If it will compile successfully it will not display any message.
  8. Then if you will check the directory containing .java file there you will find one more file with extension .class. (class file generated by compiler).
  9. Now when to run this class you need to use command as follow:java Abc   (no file extension)
  10. But you will get an error as follows:
    java.lang.NoSuchMethodError: main Exception in thread "main" 
    (will explain these all errors in coming post)  
After performing this simple practice you can guess what our compiler does and what it requires ? but to run the generated .class file java runtime system environment requires some more line of codes or you can say some operation to perform. so what it requires ?
After looking at the error(NoSuchMethodError) we can predict it needs some method or some behavior to run the java program and our answer is java runtime system environment is looking for method name "main". 

so our complete java program will be like :

public class Abc {
 public static void main(String[] args) {
  System.out.println("hello java");
 }

}
Now you can edit the code that had written earlier and repeat all those steps again and then you will find output as "hello world".
In coming post i will explain what is this main method? why it has such keyword attach to that? and many more concepts related with oops and java
 

1 comment:

  1. The Explanation is really simple and useful. Thanks Rishi for sharing such articles.

    ReplyDelete