Friday, May 28, 2010

What is System.out.println() ?And How It Is work ?

System is class of Lang Package.
System class is final .you can not able to extends to this Class .
The Constructor of system class is private .So you can not initialize this class .
PrintSteam is class in io package for responsible for give out and take input to console .
println() method is define in PrintSteam Class not System class .
and one of the limitation for PrintStream class is that you can not direct used PrintStream method with out help of System class .
We call all PrintStream method using System class .
out is reference variable of Print stream class and out is belonging to class not object .it means out is static .out is static,final reference variable of printstream .
Declaration of out In system is following
public final static PrintStream out;

Demonstration Of System.out.println()

1-We need a class which Proto type same as System class .
Class Name is WorkAsSystem ;
and constructor is private WorkAsSystem() ;

-------------------------------------------------------------------------------
package lang.example;//package like java.lang

public final class WorkAsSystem {//Similar To System Class

public static final WorkAsPrintStreamoutexample=forCreateNewWorkAsPRintStreamObject();//similar to out variable
private WorkAsSystem() { }
private static WorkAsPrintStream forCreateNewWorkAPRintStreamObject()
{
return new WorkAsPrintStream();
}

}
-------------------------------------------------------------------------------
2-We need as class which Proto type is PrintStream class
Class name is :WorkAsPrintStream

3-We also define a method same as println() in this class
method name is printlnexample();

-------------------------------------------------------------------------------

package io.example;//package like java.io ;

public class WorkAsPrintStream {//similar to PrintStream
public WorkAsPrintStream() {

}
public void printlnexample()
{
System.out.println("This is example of System.out.println();");
}
}

--------------------------------------------------------------------------------
4-Create a class which used this method as System.out.println();

--------------------------------------------------------------------------------
package example.example;

import lang.example;

public class Example
{

public Example()
{}
public static void main(String[] args)
{
WorkAsSystem.outexample.printlnexample();
}
}
--------------------------------------------------------------------------------
5-out is
--------------------------------------------------------------------------------

This is example of System.out.println();

--------------------------------------------------------------------------------

* if you can define a variable as a final then you need to initialization as define time . so out is final then we need to initialization by a private static method which is just create a new Object for PrintStream Class . to help to call method of printstream class .