Creating our first file is fairly simple. Using an IDE like Visual Studio Code is a good place to start. Eclipse is another popular IDE, as are some others.
However, you only have to have a plain text editor if you want. You can use a command line compiler and run time tool if you want, however, few businesses use that, as the IDE gives them tools to help code faster, debug better, and share code between teams, so that is what we will demonstrate.
First you will need to create a file and give it a name. In Java, you should have the first letter of each word capitalized, and not use any spaces in the file name. The file should end with a java extension, so for example Sample1.java
.
Inside that file, you will need to have several things, all of which is setting up your very first Java class. If you are using an IDE, then you may have this created for you, especially if you used their own “add new class” feature, or whatever they may call it.
What you will see, should look something like what you see below:
public class Sample1 {
}
Let’s break down each piece.
Accessor
The first thing you see is the accessor. That is public
. This says who can access the class. As a public class, what you will see most often with a class, you are allowing anyone to access create an object from the class. (We’ll talk about objects vs classes later.)
Other options in Java include, private
, protected
, and package
.
Datatype
class
is a keyword which tells Java you are defining a class. While class is the most common datatype to go here, there are others, such as interface and enum. We’ll be looking at those datatypes latter.
Class Name
This is where you define your class name. In Java there is a standard naming convention, and it is similar to the one used in C++. Classes start with a capital letter, and each word in a class name will have a capital letter. Spaces are forbidden, and underscores are discouraged, but technically legal.
Class Body
The class is contained within the braces {}
. Everything needs to be within those braces, anything outside can generate and error.
What goes in that body will vary based upon the needs of the program. We will be focusing on building out the main
function as well as class attributes and methods in up coming notes.
Differences from C++
Now, if you’ve experienced C++, you’ll notice some immediate differences. And these are good to point out in case you missed a couple.
Main Function
At this point, we don’t have a main function. This may or may not exist in this class as we work on it. Typically only one file in a project will have a main function. Whether this will be the file is yet to be seen.
No Header File
One thing, if you’ve come from programming in C++, that you won’t be “missing” is the missing header files. Header files were used in C/C++ because of limitations of the compilers and the computers they rand on back in the day. However, with Java there is no need for a header file, do no function/method prototypes.
Classes are Required
Another thing you might notice is that you had to have a class. While C++ doesn’t require the use of a class, even the most basic of files, like this one, will still have a class.
Compilation Differences
If you compile this, nothing is really going to happen. There is no main function, and no class attributes or methods that another function could call. However, you can still compile this code.
However, instead of compiling into an EXE like you would with a C++ file/project, you will get a .class
file. A class file is Java source code that is compiled into bytecode. Bytecode is the instruction set for the Java Virtual Machine. When you run a Java file, a Java run time creates a virtual machine to run your program. This virtual machine should be “nearly” identical on all machines, regardless of the OS, and allow you to “write once, run anywhere”.
Creating our First Java File was originally found on Access 2 Learn
2 Comments
Comments are closed.