简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM