Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a relationship between System class and PrintStream Class

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 ?

like image 378
Aanshi Avatar asked Oct 02 '22 15:10

Aanshi


2 Answers

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.

like image 171
Pulipati Prasadarao Avatar answered Oct 12 '22 11:10

Pulipati Prasadarao


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.

like image 26
Engineer Avatar answered Oct 12 '22 10:10

Engineer