I recently began learning Java with an online tutorial series. The website provides an online Java editor and interpreter, as they do not want viewers needing to install any programs. However, I would like to follow along in Sublime Text 3.
I've created a small test file (.java) of basic statements using correct syntax, except I do not have a package for it set up. When I run the file (in Sublime's Java interpreter), of course an error occurs where the package
statement should be.
The tutorial series does not speak about packages whatsoever. I've looked at numerous resources, but they tell me that a package is just a directory for related classes and how to create a package for a file using the Windows Command Prompt with javac, which I do not have.
I know a package is a folder and a directory for related classes/ class files, but:
- How do I set up a package for my class file(s)?
- Is a package even necessary if I only have one class file? I would assume so as the error is occurring where the
package
statement would be.
EDIT: Code:
package test;
public class TestFile
{
public static void main(String [] args)
{
//Declaring variables
byte a = 64;
short b = -385;
int c = 100000;
long d = 2000000;
float e = 16.5;
double f = 121.654;
boolean g = true;
char h = 'H';
String i = "Some string.";
String [] j = {"Some string.", "Another string.", "Yet another string."};
//Printing
System.out.println(i);
System.out.println("String i = " + i);
//If/ else if/ else construction
if (i.length() > 4)
{
System.out.println("String is longer than four characters.");
}
else if (i.length() == 4)
{
System.out.println("String is exactly four characters long.");
}
else
{
System.out.println("String is shorter than four characters.");
}
}
}
Error:
File "C:\Users\Owner\Desktop\Code\Java\Test.java", line 1
package test;
^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]