Java System.out.println() explained in OOP

Hi, all Java programmers must have used the "System.out.println()" method many times during their programming careers. It is used even in the HelloWorld Java code.
The most interesting part of it is that how it complies with the OOP (Object Oriented Programming) concepts. Many of us have used it, and are using it over and over again without any consideration on how this fits on with the OOP concepts. In this post I expect to give my clarification on how it is implemented.

Obviously "System" is a public class. And at first, it seems as if we were accessing a static method of the "System" class by calling System.out. But, if it was a method, we would basically call a method in the following manner.
System.out(); where we would be using parenthesis, and we do not use it in this case.
Hence the concept regarding a static method call of a "System" class fails.

We can access class variables by calling . . Given that the class variables are declared static. Consider the following example which further explains this.

public Class Car{

public static String CAR_MODEL;

/*Methods of class car*/

}

In this scenario, we can safely access the static variable from an outside class by simply calling

Car.CAR_MODEL

This is the case we use in System.out part. We are actually using a public static variable by the name "out" declared in the System class.

So, System class must be of the form;

public class System{

public static out;

/*The methods of System class*/

}

We do not know the exact type of the object "out". In other words, we do not know whether out is a String, an ArrayList or simply an Integer.

It may be of the form.

public class System{

public static String out;

/*The methods of System class*/

}

We cannot tell for sure.

So, now we are clear with what System.out means. The other part becomes quite obvious. We would be calling the method println() of the out object.

This is how far as we can guess on the OOP concepts. We do not know the exact type of the out object. All, we can say is that it has a method named println().

However, if we inspect the code of the System class, we may observe that the class type of out is actually "PrintStream" declared in the java.io package. We may also observe that it is declared as final in the actual implementation.

So, I believe this explains the OOP concepts behind the most common method we use to write something on the console. Please feel free to post your comments.

Comments

Unknown said…
ruchira it was a good attempt

i will try to explain it in better way.

System is class in java.lang.object

out is a static object of PrintStream class

static PrintStream out=new PrintStream();


now print() and println() are overloaded functions present in printStream class

you can check them here
http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html


hence this is System.out.print();

i.e calling the print() method from the predefined object in

following example will give a better understanding

example:

class one
{
public void print()
{
System.out.println("print called");
}
}

class two
{
static one o=new one();
}

public class three
{
public static void main(String[] args)
{


two.o.print();



}
}
ruchira said…
Hi Anand, appreciate ur effort. But I'd like to make one small correction to your contribution.
I think you missed out one important factor of this. That is the object "out" MUST be declared public static. Otherwise you cannot access it by saying System.out. You can only access such objects if they you are in the same java package.
The next point is that, since I want to keep this generic to all OOP languages, I ommited the explicit declaration of the class.

I think you will agree with me Anand. Nevertheless, I highly appreciate your contribution to making this more explanatory.
Sandeep said…
I agree with what both of you said but if you see system class its saying only out object of type PrintStream through that we are trying to access print method of printstream if I will directly create object of printstream and call print method it will write to a file that I declared in object declaration.


PrintStream obj = new PrintStream("E:/Java Work/DemoAA/src/sirrus/samples/admin/hello.txt");

But if I access that method through system it will print output in standard consol i.e
System.out.println();

I think system class in configured in such a way that if I access out object in system class and invoke print method it will print in output console
hema said…
nice explanation
hema said…
nice explanation
hema said…
very good explanation
hema said…
nice explanation
saleem said…
public printStream
{
public void println(-)
{
------
}
}

public System
{
public static printStream out=new PrintStream();
}

So,First we need to get access to out object since it is static so,we use System.out statement
and
now we got access to out object of PrintStream class so now we can happily call println(-) method of that class...finally the statement is System.out.println(-)
saleem said…
i think my assumption is correct
Mantu Kumar said…
Everyone is trying their best to answer this question. I think you should visit this:explain System.out.println().
It would clear all your doubts.

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline