Jump to content

how exactly System.out.println() words ?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
sharathm

sharathm

    Newbie

  • Members
  • Pip
  • 2 posts
import java.io.*;
import java.lang.*;

class dinput
{
public void show()
{
System.out.println("Java");
}
}

class dmain
{
public static void main(String a[])
{
dsystem.dout.show();
}
}



import java.io.*;
import java.lang.*;

public final class dsystem
{
public static final dinput dout;

}


I wanted to know how exactly System.out.println() works so i tried alternate program which
would run similar to System.out.println() but when i compile its giving me "variable dout might
not have been initialized".

Accordingly System is public final class under java.lang package.
out is a reference variable of the type PrintStream which is declared as public static final PrintStream out.
println() is a non static overloaded method present in PrintStream class.

My point is how can a static variable 'out' access a non static method.

So i have written a program which is very similar to it.

The above program dsystem class is placed in dsystem.java file which is similar to System.
The dmain and dinput is placed in dmain.java file where is dinput is similar to PrintStream class and show is similar to println() method.

dsystem.dout.show(); is similar to System.out.println();

But when i compile the program i get the error "variable dout might
not have been initialized".

I request someone to clarify my doubts.

#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
try this instead
public final class dsystem
{
          public static final dinput dout = new dinput();
}

you need to create your objects. dout without = new dinput(); is just null. (it's ok to write = null, for temporary solution, if you are going to initialize it later)
Compiler recognizes that you never initialize your dout variable , and tries to prevent some major bugs in you program by not running at all.

#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Sinipull has already answered why your code wasn't working, but I thought I'd offer a bit more explanation on exactly what's going on, if I may.

sharathm said:

My point is how can a static variable 'out' access a non static method.

The answer is, because there's nothing technically wrong with a static variable accessing a non-static method. "out" happens to be a static instance, but an instance nonetheless, so it has access to its own member variables and methods, both static and non-static members.

To simplify my understanding of it, I like to think of "static" variables and methods with a "god" analogy. Does "god" exist? For the sake of illustration here, let's assume "yes". Who created him? Nobody. He has always existed. (In reality, the System object sets these instances up, but just ignore this for the purposes of illustration.)

Same goes for static objects and members. No line of code in your program created them, they've sort of "always existed" from the beginning of time (the start of code execution). Even though an entity is static, it still has members within itself that exist in real context, and any statically or dynamically created objects can call upon those members (as long as they're made public).

The "god" object might have a public method called "prayTo()" which accepts a String containing the calling object's prayer. "god" exists statically, therefore no line of code in your program has to create it. However, since "god" exists statically, the object is instantiated and does exist in memory, and therefore its public members can be called upon by any objects in your code.

When your IDE throws you errors about static and non-static context, it's usually giving you the error, "Non-static method cannot be referenced from static context." So it's actually the other way around from the puzzler you initially provided. The example in our "god" analogy would be if we created a "disciple" object with a static method called "giveOffering()". We declare it static so that we don't necessarily have to instantiate a "disciple" in order to call "giveOffering()", in case we want to give an offering on behalf of no one in particular. Our "static context" error above would be given if, inside giveOffering(), we tried to call on one of the private members of disciple, say for instance, name. Since giveOffering() was declared static, it could possibly be called before any disciple is instantiated, and therefore there is no guarantee that there will even exist a name in memory. Furthermore, the static method could be called from within a static context, meaning it was called, but not on any one instance in particular, and so the method would have no way of knowing which disciple's name to reference.

I hope that clears up your confusion on static vs. non-static contexts. I've included a link to an excellent article that further explains the inner workings of System.out.println().

How System.out.println()*really*works « Lucky's Notes
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users