Basic syntax and access modifiers in Java

Yuvaraj
6 min readJan 24, 2023
The First Java Program

The process of Java programming can be simplified into three steps:

  • Create the program by typing it into a text editor and saving it as HelloWorld.java (filename.java).
  • Compile it by typing javac HelloWorld.java (javac filename.java) in the terminal window.
  • Execute (or run) it by typing “java HelloWorld” (java filename) in the terminal window.

A Java program can contain any number of classes, but at most one class can be declared as public. “If there is a public class, the name of the program and the name of the public class must be matched, otherwise we will get a compile-time error.”

  • If there is no public class, then we can use any name for the Java source file.
class A {
}
public class B {
}
class C {
}
  • If class B is declared public, then the name of the program should be B.java; otherwise, we will get a compile-time error.
  • If both B and C classes are declared as public and name of the file is B.java then we will get compile time error.
  • It is highly recommended to take only one class for source file and name of the program (file) must be same as class name. This approach improves readability and understandability of the code.

Example: The syntax of a Java program

HelloWorld

class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello, World");
System.out.println("Java is awesome!"); // It prints the text on a new line

}
}

/* Output: Hello, World
Java is awesome! */

Note: Every line of code that runs in Java must be inside a class. In this example, we named the class HelloWorld. A class should always start with an uppercase letter. Java is case-sensitive: “MyClass” and “myclass” have different meanings.

Java Comments:

Comments can be used to explain Java code and make it more readable. It can also be used to prevent execution when testing alternative code.

Single-line Comments:

Single-line comments start with two forward slashes (//). Any text between // and the end of the line is ignored by Java (will not be executed).

Multi-line Comments:

Multi-line comments start with /* and ends with */ . Any text between /* and */ will be ignored by Java.

In this example:

class is a keyword to create a class. HelloWorld is our class name.

// Valid Java main() method Signatures..

1. public static void main(String[] args)
2. public static void main(String []args)
3. public static void main(String args[])
4. public static void main(String... args)
5. static public void main(String[] args)
6. public static final void main(String[] args)
7. final public static void main(String[] args)
8. final strictfp public static void main(String[] args)

public static void main(String args[]) or static public void main(String args[]) Here,

public: It is an access modifier that allows you to access the class from anywhere in the program.

static: It is a keyword that helps the main method is to be accessible/called without creating objects of the class. The modifiers public and static can be written in either order.

void: It is a keyword that means this method will not return anything.

main(): It is the default signature defined in the JVM. The main method must be inside the class definition. It is the entry point of a Java program. The compiler executes the code, always starting from the main function. Without the main function, it will not run.

String args[]: This is the argument passed to the main function, which is an array of strings with the array name args.

System.out.println: This is used to print anything on the console, like printf in the C language. Here,

System.out: System is a built-in Java class that contains useful members, such as out, which is short for “output.” It is the standard output stream that is used to produce the result of a programme on an output device like the computer screen.

println(): This method in Java is used to display text on the console on a new line.

Note:

  • The curly braces { } marks the beginning and the end of a block of code.
  • You should also note that each code statement must end with a semicolon (;).

You can also output numbers and perform mathematical calculations:

class Main {
public static void main(String args[])
{
System.out.println(3 + 3);
}
}

// Output: 6

Note: We don’t use double quotes (“ ”) inside println() to output numbers.

There is also a print() method, which is similar to println(). The only difference is that it does not insert a new line at the end of the output.

class Main {
public static void main(String args[])
{
System.out.print("Hello Java! ");
System.out.print("This will print on the same line.");
}
}

// Output: Hello Java! This will print on the same line.

Note: For better readability, we add an extra space (after “Hello Java!” in the preceding example).

Access Modifiers

Access modifiers are the new official term used instead of access specifiers. In Java, there is no concept like access specifiers; all are considered to be modifiers only by default. Access specifiers and access modifiers both mean the same thing.

List of Access Modifiers in Java

Access modifiers are used to set the accessibility of classes, interfaces, fields, methods, constructors, etc. They define how they can be accessed from other parts of the program.

In general, there are four access modifiers in Java. They are,

  • Public
  • Private
  • Protected
  • Default
Accessibility of Access Modifiers

Public: It has no scope restriction; we can access data items and functions from anywhere in the program.

Private: It cannot be accessed outside of the class; it can only be accessed from the class in which it is defined. To access the variable from the outer class, we use getter and setter methods in Java.

Protected: It can be accessed from classes that belong to the same package, subclass, or from within the class where they are defined.

Default: It can be accessed from all the classes that belong to the same package. If we do not explicitly specify any access modifier for classes or methods, then by default it will be considered.

class Animal {
public void method1()
{ ... }
private void method2()
{ ... }
}

In this example, we do not specify any access modifier for the class, so it will be considered the default. Here, method1 is public, which means it can be accessed by other classes. Also, method2 is private, which means it cannot be accessed by other classes.

class Parent {
protected int x = 10;
}

class Child extends Parent {
public void printX() {
System.out.println(x);
}
}

public class Main {
public static void main(String[] args) {
Child c = new Child();
c.printX(); // prints 10
}
}

In this example, the variable “x” in the parent class is marked as protected. This means that it can only be accessed by the parent class, any classes that extend the parent class (such as child classes), and any classes in the same package as the parent class. In this case, the child class is able to access the protected variable “x” and print its value.

--

--