Java Non-Access Modifiers or Non-Access Controls

Java non-access modifiers do not control access level, but provides other functionality like:

  • to control inheritance capabilities,
  • to check whether all objects of the class have their own values or share the same member value,
  • to check whether a method can be overridden in a subclass.

Java non-access modifiers are categorised separately for classes & attributes and methods level as follows:

For class:
A class can be final:
Final class cannot be inherited by other classes or we can also say, output of final class can not be overriden.

A class can be abstract:
If a class is defined as abstract then it cannot be used to create objects or in other words, in order to access an abstract class, it must be inherited from another class defined in same program of outside of program using inheritance.

For attributes and methods:
final:
Attributes values can’t be changed once assigned and methods cannot be overridden.

static:
Attributes and/or methods belongs to the class, rather than an object of that class.

abstract:
Can only be used in an abstract class, and can only be used on methods. If a method is defined abstract, it has to be implemented in a subclass, which contains abstract methods (a process of abstraction and inheritance).

transient:
Attributes and/or methods are skipped when serializing an object containing them.

synchronized:
synchronized controls thread access to a block/method. Methods can only be accessed by one thread at a time.

volatile:
Attribute’s value is not cached thread-locally, and is always read from the “main memory” (not from a specific thread’s memory).

native:
native is a simple keyword that marks a method that will be implemented in other languages, not in Java. It works together with the Java Native Interface (JNI).

In short, we have only 6 working non-access modifiers in Java: static, final, abstract, synchronized, volatile and transient.

See Java Access Modifiers or Access Controls

Java Access Modifiers or Access Controls

Properties (variables and constructors), classes and methods (functions) can have access modifiers to define where they can be accessed. In other words, Access Modifiers are controllers which control a property or method to be accessed. Access modifiers, best described on Oracle Docs

Java access controls are categorised in two levels:
At the top level: public, or package-private (no explicit modifier).
At the member level: public, private, protected, or package-private (no explicit modifier).

In Java, there are four access modifiers:
default: when there is no access control defined with a property or method, it is treated as default modifier. Default modifier is only accessible within the package, it cannot be accessed from outside the package. The default access modifier is also known as package-private.
public – when a property or method is defined with public access control, it can be accessed from everywhere in the same package or outside of the package.
protected – when a property or method is defined with protected access control, it can be accessed within the class and by classes derived from that class.
private – when a property or method is defined with private access control, it can ONLY be accessed within the class.

Below tables well explains Java Access Levels of different entities around Java world:

ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN
This table defines levels of access conferred by a modifier.

Java – Download and Install JDK on Windows 10

Step 1:
You can visit Oracle Help Center for Installation of the JDK and the JRE on Microsoft Windows Platforms and to download JDK visit download page. Click on the version you want for your system, it will ask for Terms and Conditions approval, once confirmed it will start downloading files.
I have downloaded “jdk-15_windows-x64_bin.exe” then installed it in my system.

Step 2:
To install JDK, open the download folder of your windows 10 system. Double click on the executable file (in my case “jdk-15_windows-x64_bin.exe“). Windows will ask for permission to allow installation in the system, once allowed (by clicking Yes), the system will process further. Follow the steps during installation process as shown below in images:

Step 3:
After the installation is complete, delete the downloaded file to recover the disk space. (Optional)

Step 4:
You can verify your JDK installation by visiting your installation path of JDK. In my case it is “C:\Program Files\Java\jdk-15“.

Step 5:
Setting the PATH Environment Variable, so that Java is accessible globally in the system.

To set the PATH variable permanently, add the full path of the “C:\Program Files\Java\jdk-15\bin” directory to the PATH variable.

  1. Select Control Panel and then System.
  2. Click Advanced and then Environment Variables.
  3. Add the location of the bin folder of the JDK installation to the PATH variable in System Variables, as a new line entry.
  4. Click OK for “Edit environment variable window”
  5. then click OK for “Environment Variables window”
  6. then click OK for “System Properties window”
  7. and close the System Control Panel.

Java – “Hello World” program

Step 1:
We will create our first Java file, which can be done in any text editor (e.g. Notepad++, Atom, or Sublime Text).

Step 2: Below is the content of Hello World program file, all lines are required:

public class MyClass { public static void main(String[] args) { System.out.println(“Hello World”); } }

See Explanation of above program

Step 3:
Save the file as “MyClass.java” because our program name is MyClass. File name must be the same as the main function name.

Step 4:
Open a terminal (cmd, putty, git-bash), navigate to the directory where you saved your file.

Step 5:
Type following command in command line, to compile the code:

javac MyClass.java

In case if there is any syntax or code error, the command prompt will point it.

Step 6:
You will find an executable file “MyClass.class” in the directory where you saved your java program file.

Step 7:
Type following command in command line, to run the file:

java MyClass

It will show you the result of the file. In our case it is printing:
Hello World


Congratulations

Java – Explaination of Hello World program

public static void main(String[] args) { System.out.println(“Hello World”); }

Public:
Type: Access modifier
Description: In the above program using public makes main() method globally available.
Public method specifies from where and who can access the method. Public methods can be invoked by JVM (Java Virtual Machine) from outside of the class, in the same program file or any other java program file.

See Java Access Modifiers or Access Controls

Static:
Type: Keyword
Description: JVM invokes static main() method without instantiating the class which, also, in turn saves the unnecessary wastage of memory while calling object(s) declared for calling the main() method.

Void:
Type: Keyword
Return Type: Void
Description: Void is used to specify that a method doesn’t return anything.

main:
Type: Identifier
Description: JVM looks for main() as the starting point of the java program.

String[] args:
Type: Array of type java.lang.String class
Description: It stores Java command line arguments.

System:
Type: Class
Description: System is a final class in the java.lang package

out:
Type: Variable
Description: Out is a public and static member of the System class and is an instance of java.io.PrintStream

println:
Type: Method
Description: The println is a method of java.io.PrintStream, which prints any argument passed while adding a new line to the output.

© 2024 Faraz Jafri All rights reserved. Privacy Policy

Proudly powered by WordPress. Special thanks to Bootstrap, FontAwesome & Open Source Community.