Home » Java programming language

Java Class class getProtectionDomain() method with example

Class class getProtectionDomain() method: Here, we are going to learn about the getProtectionDomain() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 19, 2019

Class class getProtectionDomain() method

  • getProtectionDomain() method is available in java.lang package.
  • getProtectionDomain() method is used to return the ProtectionDomain of this class (i.e. ProtectionDomain protects source code by implementing a set of permissions).
  • getProtectionDomain() method is a non-static method, it is accessible with the class objects only and if we try to access the method with the class name then we will get an error.
  • getProtectionDomain() method may throw an exception at the time of returning ProtectionDomain of the class.
    SecurityException : In this exception its checkPermission() method restricts ProtectionDomain when security manager exists.

Syntax:

    public ProtectionDomain getProtectionDomain();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is ProtectionDomain, it returns the ProtectionDomain of the class.

Example:

// Java program to demonstrate the example 
// of ProtectionDomain getProtectionDomain() method of Class 

import java.security.*;

public class GetProtectionDomainOfClass {
    public static void main(String[] args) throws Exception {
        // Get Class object
        Class cl = Class.forName("java.util.ArrayList");

        // It return the ProtectionDomain of the class ArrayList
        ProtectionDomain pd = cl.getProtectionDomain();

        // Display ProtectionDomain of the class
        System.out.print("ArrayList ProtectionDomain : ");
        System.out.println(pd);
    }
}

Output

ArrayList ProtectionDomain : ProtectionDomain  null
 null
 <no principals>
 java.security.Permissions@2a33fae0 (
 ("java.security.AllPermission" "<all permissions<" "<all actions>")
)


Comments and Discussions!

Load comments ↻





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