Java Beginners - Java For Beginners Java File Structure Rating: 1.0/5 (1 vote cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Java Source Files - The Structure of Java Source FilesThere are two types of files you will work with in Java. The Java source file (.java) is created by programmers and contains human-readable instructions. The second file is the Java classfile (.class). The Java classfile contains binary instructions called bytecode that are executed by the Java Virtual Machine (JVM). Each .java source file contains, at most, one top-level public class definition. If a public class is present, the class name must match the unextended filename. For example, if a source file contains a definition for a public class BusinessReport, then the source file must be named BusinessReport.java. A source file may contain any number of non-public class definitions. When your .java file is compiled, a matching .class file is generated with an identical name. For example, BusinessReport.java will be compiled into BusinessReport.class. C:\source>javac BusinessReport.java Classfiles are read by the Java Virtual Machine when the java command is invoked. C:\source>java -cp . BusinessReport Each Java source file may contain any of the following three top-level elements. None of these elements are strictly required by the compiler. - package declaration: If included, the package declaration must be the first statement in the file. The package keyword is followed by a package name. The package name is a series of elements separated by periods. Each period separated element must correspond to a filesystem subdirectory under which the classfile is located. For example, if a class was declared to be in the com.taylor.gui package, it would be located in the com/taylor/gui/ subdirectory. Only one package declaration is allowed per .java file.
- import statements: Imports are similar to package declarations in structure. Import statements point to classes or packages that should be made available for use witin the current class. For example, in order to use the java.applet.Applet class in a Java file, the class would have to be imported via the import java.applet.Applet; or import java.applet.*; statement. Import statements are not recursive, so import java.*; does not import all classes in subpackages of java.
- class definition: The class definition contains both the class visibility and the class name. It may in turn contain any number of child elements wrapped by curly braces. Remember that each source file must, at the most, one public class.
A class can contain several different types of elements in addition to the three top-level elements listed above. The two most important of these are attributes and methods. Attributes are variables that have a visibility, type, and value, such as public and integer and 33, or private and String and "Hi, Mom!". Methods are class behaviors that accept data as method parameters, perform some operation on the data, and return a value. Methods have a visibility, return type, name, and list of parameters. package arunsoft; import java.lang.System; /** * Do not enter the line numbers * * The OnePlusOne class adds 1 + 1 */ public class OnePlusOne { private static int one = 1; public static void main(String args[]) { System.out.println(one + one); } }
Type the above source code into a file named OnePlusOne.java and save the file in a subdirectory named mysource. Then compile and execute the code. Make sure you are in the immediate parent directory of mysource. C:\source> javac arunsoft/OnePlusOne.java C:\source> java arunsoft.OnePlusOne 2
In OnePlusOne.java above, line 1 contains the package declaration. Line 3 imports the System class so that it can be used to print out the results of the addition operation on line 15. Line 10 declares the OnePlusOne public class. Line 12 declares the public static int variable, one. Line 14 declares the main method. Line 15 prints out to the computer's standard output stream the result of adding 1 + 1. Additional syntax notes: - Class and method bodies are enclosed by curly braces.
- All statements are ended with a semicolon.
- Whitespace is ignored unless it is between quotation marks or single quotes.
1 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|