I'm trying to know what exactly System.out.println(). I read these two articles
What is System, out, println in System.out.println() in Java and What's the meaning of System.out.println in Java?. I know what is System,out,and print, But i don't know how System class can connect to PrintStream class
. How they are related to each other
?
System is a class in the java.lang package.out
is a static member of the System class, So how its becomes an instance of java.io.PrintStream
?How System and PrintStream are related to each other ?
The relation between System class and PrintStream class is HAS-A relation. Here System class HAS-A PrintStream class. To understand the relation understand the program.
class A
{
void display()
{
System.out.pritln("this is display method");
}
}
class B
{
static A ob=new A();
}
class demo
{
public static void main()
{
B.ob.display();
}
}
It prints this is display method.
B.ob.display() is just like System.out.println().
A object is created in B class.
PrintStream class object is created in System class.
ob is static object reference of A class.
out also static reference of PrintStream class.
System
class has static object of PrintStream
class which is declared in System
class as out
and the println()
is the method of PrintStream
class.
So we can access static object as System.out
and the println()
is the method of PrintStream
class. That's why we can write System.out.println()
and how both classes are related.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With