This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output missing something... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Output missing something..." Watch "Output missing something..." New topic
Author

Output missing something...

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
Hello all...
Can someone explain me why in the below program, the Output
does not print the Char 'V'.
class Process
{
boolean b;
public Process(int x)
{
System.out.println((char)x);
}
}
public class Test
{
Process p3 = new Process('V');

public static void main(String[ ] args)
{
Process p4 = new Process('X');
}
static Process p1 = new Process('J');
static Process p2 = new Process('$');
}
The Output of the Above Prog:-
J
$
X
Why doesn't the Char 'V' is not included in Output ?
Thanks.

Shah Chunky - Sun Certified Java2 Programmer.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Shah Chunky:
Hello all...
Can someone explain me why in the below program, the Output
does not print the Char 'V'.
public class Test
{
Process p3 = new Process('V');

public static void main(String[ ] args)
{
Process p4 = new Process('X');
}
static Process p1 = new Process('J');
static Process p2 = new Process('$');
}
The Output of the Above Prog:-
J
$
X

In your Test class the variable p3 is an instance variable so it is never created until you create an actual Test object. The variables p1 and p2 are static so they are created and instantiated when the Test class is loaded. Then in main you specifically create a Process object so it too prints. To see the 'V' printed, just create an instance of your Test class in main.
add a line like this:
Test newTest = new Test();
and then V will print.
hth
Dave

Dave
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Output missing something...