Java Classes
A class can be defined as a template/blueprint from which individual objects are created.
Starting a Java Program:
- When we write code in Java, we will be writing classes or interfaces.
- A class code starts with a { and ends with a }. A class contains variables and methods. Let’s create main() method which is compulsory method.
/* My First Java Program */ Import java.lang.system; Import java.lang.String; Class MyFirstJava { public static void main (String[] args){ System.out.println(“Welcome to Java Programming”); } }
Why main() method is compulsory method ?
If main() method is not written in Java program, it won’t be executed by JVM. The main() method is the starting point for JVM to start the execution of a Java program.
Using public static void main (String[] args)
- The main() method can accept some data from outside and can return some result also. For example, it accepts group of strings, which is also called as string type array String[] args . This array is written along with the main method.
- Here args[] is the array name and it is of String type. So it can store group of strings.
- The value passed to the main() method are called arguments. These arguments are stored in args[] array.
- Other versions of main() method with other signatures are perfectly legal, but they are treated as normal methods.
- The following are legal declarations of the “special” main method (the one used to start a Java program)
Static public void main (String[] args) Public static void main (String… x) Static public void main (String var_args[])
- For the certification exams, what you need to know is that main() method can be overloaded.
- Static methods are the methods which can be called and executed without creating objects. JVM calls the main() method using its class name as MyFirstJava.main() at the time of running the program.
- The main() method must be declared as public, so that it would be available for the JVM. If we don’t declare main() method as public then it won’t be available to JVM and JVM cannot execute it.
- import statements make the JVM to go to the specific Java standard libraries, execute the code there, and substitute the results into the program.
import java.lang.System; import java.lang.String;
Using the javac and java commands
The javac command is used to invoke the Java’s compiler.
javac MyFirstJava.java
The compiler generates a file called MyFirstJava.class that contains byte code instructions. This file is executed by JVM using the following command
java MyFirstJava
Then, we can see the following result:
Welcome to Java Programming.
Classes – Declaration Rules:
The following are the rules to be followed while declaring classes
- Only one public class per source code file is allowed.
- Comments can be added at the beginning or end of any line in the source code.
- The name of the file must match the name of the public class in a file.
- The package statement must be the first line in the source code file before any import statements.
- Import statements must be added in between the package statement and the class declaration.
- If there is no package statement, then import statement must be the first line in the source code file.
- If there are no package and import statements, then the class declaration must be the first line in the source code file.
- Import and package statements apply to all classes present in the source code file.
- A source code file can have more than one non-public class.
- Source code files with no public classes can have a name which does not match with any of the classes in the file.
Its a nice and helpful piece of info.