Friday, June 19, 2009

Java Source File Declaration Rules

Declaration Rules

Before doing the hardcore programming one has to be familiar with the rules which govern the source file declaration in Java.Sun Microsystems has provided the guidelines for the naming and declaration.Here we will look the rules which are associated with declaring classes,import statements etc.

Rule 1:- There can be only one public class per source code file(e.g. a Java File).

Rule 2:-If there is a public class in a file then the name of the java file must match the name of public class.For example if you declared public class Man{ } then it must be stored in a file which is also named as Man.java.

Rule 3:-Comments can appear at the beginning or end of any line of code of source code file.

Rule 4:-A source file can have more than one non-public classes.

Rule 5:-If a class is a part of a package,then the package statement must be first line of code,irrespective of any import statement present.
For example:-
package cert;
import java.lang.*;

Rule 6:-If there are import statements then they must go between package and class declarations.If there are no package statements then import statement must be first line of code and if import is also absent then class declaration will be first statement in source file.

Rule 7:-import and package statement apply to all classes within a source file.In other words there in no way to declare multiple classes in a file which have them in different packages and using different imports.

Rule 8:-Files with no public classes can have a name which may or may not match the name of any constituent class.

Home
India Online

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey(Read More about him) .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP