0

I have read that System is a class, out is a object and print is a method but to call a method, we simply need to use object then why should we use System. Can anyone please tell me.

user3679701
  • 45
  • 1
  • 1
  • 7
  • 2
    `out` is `static` property of `System`, it's like a container, in which `out` resides, so to reference `out`, you need to specify the container. Now you could cheat and use `import static java.lang.System.out;` which will allow you to use `out.print` within your code directly... – MadProgrammer May 29 '14 at 08:36
  • @KishanSarsechaGajjar have u even read that question ?? There is no similarity between the two questions – user3679701 May 29 '14 at 08:49

5 Answers5

3

Because that's the way we access to static members of a class. In this case, the class is System and its static member out is declared as

public final static PrintStream out = null;

Since it's public we can access to it directly, but using the notation SomeClass.someStaticMember

System.out

and if we want to call a method of that object (because that member is an object), we have to call it as

System.out.println()
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

System – is a final class and cannot be inherited. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”

out – is a static member field of System class and is of type PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data.

println – println prints the argument passed to the standard console and a newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.

An Example to explain

Let me try explaining this way.

How do you use a convert an String to an int?

We use Integer.parseInt();

Here parseInt() is a static method in the Integer class.

Similarlly, out is a static Member of type PrintStream in the System class.

public final static PrintStream out = nullPrintStream();//line 82

Hence we use System.out

println() is a method os PrintStream hence we use System.out.println()

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
NewUser
  • 3,729
  • 10
  • 57
  • 79
1

If you don't like to write System everytime, you could import it:

import static java.lang.System.*;
public class Main {
    public static void main(String[] args) {
        out.print("Hello, world!");
    }
}

But usage of System.out.print() seems to be a common habit already.

arghtype
  • 4,376
  • 11
  • 45
  • 60
0

System is a class and out is a static member field that System contains

Juanpe
  • 446
  • 5
  • 16
0

out is a variable(ref variable) that is declared in System class

public final static PrintStream out = null;

as it is static variable you need to call it through class name.As System class's constructor is private you can not create the object of System class & you can't call it through reference .There is a way you can directly call out.println by using static import..refer bellow example ..

import static java.lang.System.out;
 public class MainTest {

    public static void main(String[] args) {
     out.print("Hello");
    }

 }

Hope it may help .

Ashok_Pradhan
  • 1,159
  • 11
  • 13