Java/J2EE and SOA (346 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (343 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is System Class in Java and how to implement it?

Last updated on Nov 26,2019 6.7K Views

Swatee Chand
Sr Research Analyst at Edureka. A techno freak who likes to explore... Sr Research Analyst at Edureka. A techno freak who likes to explore different technologies. Likes to follow the technology trends in market and write...
8 / 11 Blog from Objects and Classes

Java provides us with a comprehensive set of pre-built classes and libraries which reduces the need of overhead coding. One such class is System class in Java. In this article, I will be talking about various concepts that constitute this class and how they make it one of the most widely used among Java Developers.

Below are the topics I will be discussing in this article:

Let’s get started.

System Class in Java

The System is one of the core classes in Java and belongs to the java.lang packageThe System class is a final class and do not provide any public constructors. Because of this all of the members and methods contained in this class are static in nature. Thus you cannot inherit this class to override its methods. Since the System class in Java comes with so many restrictions, there are various pre-built class fields and methods available. Below I have listed down a few of the important features supported by this class:

  • Standard input and output
  • Error output streams
  • Access to externally defined properties and environment variables
  • Built-in utility for copying a part of an array
  • Provides means for loading files and libraries

Now that you are aware of what exactly is System class in Java, let’s move ahead and find out, how to declare this class.

java.lang.System Class Declaration

Below I have demonstrated the declaration for java.lang.System class:

public final class System extends Object

The System class in Java comes with various inbuilt class fields and methods. Let us now move further in this article and learn about them one by one, starting with the class fields.

Class Fields

The java.lang.System class comes with three fields which are:

  1. public static final InputStream in: This is the standard input stream in Java programming. This stream is already open and available for supplying the input data. This input stream mainly corresponds to the keyboard inputs or other input sources that are specified by the host environment or a user.
  2. public static final PrintStream out: This is the standard output stream in Java programming. This stream is already open and available for accepting the output data. This output stream mainly corresponds to displaying the output or another output destination that is specified by the host environment or a user.
  3. public static final PrintStream err: This is the standard error output stream in Java programming. This stream is already open and available for accepting the output data. This output stream mainly corresponds to displaying the output or another output destination that is specified by the host environment or a user. Technically, this output stream is used for displaying the error messages or other information that needs the immediate attention of a user.

Now that you are aware of the class fields of System class in Java, let’s now take a look at the various methods provided by this class.

System Class Methods

There is a total of 28 in-built methods declared in the java.lang.System class. Below I have listed down each of them along with their explanations.

MethodDescription
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)This method helps in copying an array from the specified source array, starting from the specified position, till the specified position of the destination array.
static String clearProperty(String key)This method helps in removing a system property which is indicated by the specified key
static Console console()This method helps in returning any available unique Console object that is associated with the current JVM
static long currentTimeMillis()This method helps in returning the current time in milliseconds
static void exit(int status)This method helps in terminating the currently running JVM
static void gc()This method helps in running the garbage collector
static Map<String,String> getenv()This method helps in returning an unmodifiable string map view of the current system environment
static String getenv(String name)This method helps in retrieving the value of the specified environment variable
static Properties getProperties()This method helps in determining the current system properties
static String getProperty(String key)This method helps in retrieving the system property that is indicated by the specified key
static String getProperty(String key, String def)This method helps in retrieving the system property that is indicated by the specified key
static SecurityManager getSecurityManager()This method helps in retrieving the system security interface
static int identityHashCode(Object x)This method helps in returning the same hash code for the given object whose value will be similar to the default method hashCode(), irrespective of given object’s class overrides hashCode()
static Channel inheritedChannel()This method helps in returning the channel which is inherited from the entity that created JVM
static String lineSeparator()This method helps in returning the system-dependent line separator string
static void load(String filename)This method helps in loading a code file with the specified filename from the local file system as a dynamic library
static void loadLibrary(String libname)This method helps in loading the system library specified by the libname argument
static String mapLibraryName(String libname)This method helps in mapping a library name into a platform-specific String representing a native library
static long nanoTime()This method helps in returning the current value of the running JVM’s high-resolution time source within nanoseconds
static void runFinalization()This method helps in executing the finalization methods of any objects pending finalization
static void setErr(PrintStream err)This method helps in reassigning the “standard” error output stream
static void setIn(InputStream in)This method helps in reassigning the “standard” input stream
static void setOut(PrintStream out)This method helps in reassigning the “standard” output stream
static void setProperties(Properties props)This method helps in setting the system properties to the Properties argument
static String setProperty(String key, String value)This method helps in setting the system property indicated by the specified key
static void setSecurityManager(SecurityManager s)This method helps in setting the System security
static void runFinalizersOnExit(boolean value)Deprecated

Let’s now try to implement some of these methods of System class in Java in the next section of this article.

Implementing System class in Java

In the following example, I have implemented a few of the above-discussed methods.

package edureka;

import java.io.Console;
import java.lang.*;
import java.util.*;

public class SystemClassMethods {

	public static void main(String[] args) {
		String a[]= {"D","P","R","E","K","A"}; //source array  
        String b[]= {"E","D","U","V","O","I","D","L","E","A","R","N","I","N","G"};  //destination array  
        String src[],dest[];  
        
        int srcPos,destPos,length;
        src=a;
        srcPos=2;
        dest=b;
        destPos=3;
        length=4;
        
        System.out.print("Source array:"); 
        
        for(int i=0;i<src.length;i++) {System.out.print(a[i]);}  
        System.out.println(); 
        
        System.out.print("Destination array:");         
        for(int i=0;i<dest.length;i++) {System.out.print(b[i]);}  
        System.out.println();  
        System.out.println("Source Position:"+srcPos);  
        System.out.println("Destination Position:"+destPos);  
        System.out.println("Length:"+length);  
        System.arraycopy(src, srcPos, dest, destPos, length); //use of arraycopy() method 
        
        System.out.println("After Copying Destination Array: "); 
        for(int i=0;i<b.length;i++)  
        {
        	System.out.print(b[i]);  
        }  
        System.out.println();
        
        
        System.out.println("---------Implementing NanoTime Method----------");
        System.out.println("Current time in nanoseconds = "+System.nanoTime());  

        
        System.out.println();
        System.out.println("---------Implementing getProperties() Method----------");
        System.out.println("Your System property for user");  
        Properties p = System.getProperties();  
        System.out.println(p.getProperty("user.name")); //property to get User's account name  
        System.out.println(p.getProperty("user.home")); //property to get User's home directory  
        System.out.println(p.getProperty("user.dir")); //property to get User's current working directory 
        
        System.out.println();
        System.out.println("---------Implementing console() Method----------");
        Console console = System.console();

        if(console != null){
            Calendar c = new GregorianCalendar();
            console.printf("Welcome %1$s%n", "Edureka"); 
            console.printf("Current time is: %1$tm %1$te,%1$tY%n", c); 
            console.flush();
        } else{
        	//No console is attached when executed in Eclipse
        	System.out.println("No Console attached");
        }
        
        System.out.println();
        System.out.println("---------Implementing getSecurityManager() Method----------");
        SecurityManager secManager = System.getSecurityManager();
        if(secManager == null){
        	System.out.println("SecurityManager is not configured");
        }
        SecurityManager mySecManager = new SecurityManager();
        
        System.setSecurityManager(mySecManager);
        secManager = System.getSecurityManager();
        if(secManager != null){
        	System.out.println("SecurityManager is now configured");
        }        
   }

}

Output

Source array:DPREKA
Destination array:EDUVOIDLEARNING
Source Position:2
Destination Position:3
Length:4
After Copying Destination Array: 
EDUREKALEARNING

---------Implementing NanoTime Method----------
Current time in nanoseconds = 433367948321300

---------Implementing getProperties() Method----------
Your System property for user
Swatee_Chand
C:UsersSwatee_Chand
C:UsersSwatee_Chandeclipse-workspaceSystemClass

---------Implementing console() Method----------
No Console attached

---------Implementing getSecurityManager() Method----------
SecurityManager is not configured
SecurityManager is now configured

You can try to implement rest of the methods and in case you get stuck somewhere, you can drop a comment and we will help you with it.

With this, we come to the end of this article on System class in Java. If you want to know more about Java you can refer to our other Java Blogs.

Now that you have understood what is System class in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “System class in Java” article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is System Class in Java and how to implement it?

edureka.co