Home » Java programming language

Java System class setSecurityManager() method with example

System class setSecurityManager() method: Here, we are going to learn about the setSecurityManager() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 12, 2019

System class setSecurityManager() method

  • setSecurityManager() method is available in java.lang package.
  • In setSecurityManager() method, if the security manager is already installed then in that case the given parameter establish a connection as the current security manager else if the given argument is null then in that case the given parameter couldn’t establish a connection as the current security manager then no action will be performed.
  • setSecurityManager() method is a static method so it is accessible with the class name too.
  • setSecurityManager() method does not throw any exception.

Syntax:

    public static void setSecurityManager(SecurityManager sm);

Parameter(s):

  • sm(security manager) – represents the security manager if the security manager couldn’t establish a connection previously for the current application.

Return value:

The return type of this method is void, it does not return anything.

Example:

// Java program to demonstrate the example of 
// setSecurityManager () method of System Class.

import java.lang.*;
public class SetSecurityManagerMethod {
    public static void main(String[] args) {
        // Creating an instance of SecurityManager
        SecurityManager sm = new SecurityManager();

        // By using setSecurityManager() method is used to 
        //set the Security Manager
        System.setSecurityManager(sm);


        // By using getSecurityManager() method is used 
        //to get the Security Manager
        sm = System.getSecurityManager();

        // By using conditional statement to check existence 
        //of Security Manager existence

        if (sm == null)
            System.out.println("Security manager couldn’t established");

        else
            System.out.println("Security manager connection is established");
    }
}

Output

E:\Programs>javac SetSecurityManagerMethod.java
E:\Programs>java SetSecurityManagerMethod
Security manager connection is established



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.