How to create a basic Java File!
*Note you will need a Java Development Environment(JDE) and the Java Development Kit*
**Recommended JDE( JGrasp )**
*****COMMENT FOR ACCESS TO INFORMATION!******
Step 1: Creating the file!
- You are going to want to open JGrasp and create a New file.
- This can be accomplished by hovering over "File" and then hovering over "New".
- Once you are hovering over "New" Select Java.
Step 2: Naming a file!
- To name a file you must declare whether or not the class is Public* or Private*
- Then type a class name that you want to set the FileName as.
- For the example(1) we will define the class as Public and the FileName as Hello
public class Hello
Step 3: Understanding bracket usage!
- Brackets are used to encase ( set code to ) code to either a Class or a Method.
- Classes contain methods.
- For the example(2) we will use brackets to start the class.
- This bracket will start the execution of the class file.
public class Hello {
Step 4: Adding methods!
- Methods contain actual coding, and act like an action ( they do something.)
- You want to include the commands and coding you want to use within a method.
- Java files will generally start the process of code execution through a Main method.
- Main methods are necessary to the execution of a class(es)
- Main methods must be void methods (meaning they return no value.*)
- *We will get to return methods in a later lesson.*
- For the example(3) we will include and demonstrate the main method.
public class Hello { public static void main(String[] args) {
- **NOTE** Methods - just like the class declaration - have to be followed by an { bracket, it will start the code execution!.
Step 5: Adding commands / variables to Methods!
- Methods can contain both commands and variables.
- Variables have to be defined using: String(for anything included in "double quotes"), int (whole numbers 1,2,3,4...), double (integers with decimals ie. 1.0, 2.3, 5.2...), boolean (T/F), char ( any letter with ' ' single quotes)
- Variables can be defined to another keyword that isn't Java Specific (for example- int Dog; Cat = Dog; . Dog = 4; ) (In this example Dog would be an integer of 4, and Cat would equal Dog or the value 4.)
- Methods will take Java Commands and computations.*
- *Refer to a search / keyword database.
- For this example(4) we will complete the method for Main (refer to ex. 3) ?
public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); System.out.println("This is a test"); }
- System.out.println will call the computer to print the strings provided in the argument field( the area in between the () parenthesis.)
- **NOTE** The method is closed by a } bracket
Step 6: Adding a EOF(end of File) declaration!
- To complete a file you will need to add another bracket }, all brackets must have a partner!
- For this example(5) we will show the bracket Pairs. *THERE ALWAYS HAS TO BE AN EVEN NUMBER OF BRACKETS* ( in EX.4 there is 4!)
- The inclusion of the final bracket ends the Class File.
public class Hello { public static void main(String[] args) { System.out.println("Hello World!"); System.out.println("This is a test"); } }