About Me

My photo

I'm an undergraduate of Rajarata University of Sri Lanka reading Applied sciences degree. I'm really interested in Java Programming language and developing some software using Java

Friday, July 8, 2011

Basic Java Class

Hi! Today I'm going to talk some thing about the basic java source file
We know all programming languages has to compile before they execute to change the platform.
Java source file can be write in notepad or any other IDEs like NetBeans and what ever the IDE or notepad we are using we have to follow some restrictions to make compile our code.
In our source file the first part that is the top part of the source file must be the package declaration part which declare our source file to a particular place where some other source files also can be included.

The second part will be the import statement part which tell the compiler some source file we have to attached from other packages to our package because we are using the source code of that particular package.

The third thing i the class declaration where our favourite java class going to be declared.

If we don't have the first part and second part we talked above. The class declaration will be the first part

for example :

class Test{

The above code just compile fine and can't run the file because the JVM looks for the main method for starting the execution our above code don't have main method so at the runtime JVM pass an exception.

If we make the class as public we have to have the sourcefile name as same to the public class name. One java file can contain more than one class but only one class can declare as public and if a class declared public the source file must be named as the public class name

Full Example

package myPackage;//package declaration

import java.util.*;//import statement
import java.io.*; //import statement

public class A{
B b = new B();

class B{
Random random = new Random();

the above class must be named as A.java